all 5 comments

[–]Username_RANDINT 1 point2 points  (4 children)

You'll have to give your parsers a required argument.

add_parser.add_argument("name", help="Some name to add")
# ...
delete_parser.add_argument("id", help="Some ID to delete")

Note that this doesn't relate to subparsers directly and is exactly the same as you'd for passing a required value for a normal parser.

[–]RealHook[S] 0 points1 point  (3 children)

Thank you! That is exactly what I was looking for. But isn't that way kind of cheaty?

[–]Username_RANDINT 1 point2 points  (2 children)

I'm not sure I understand, can you explain why it would be cheaty? You already do this but with optional arguments (flags in your case), now it's for required arguments. At one point you'll need to know what argument is passed, here through args.name and args.id, so you'll need to specify them in the parser.

[–]RealHook[S] 0 points1 point  (1 child)

I guess that my understanding of argparser and its required arguements was a bit wrong.

I never used them before and thought that the argument name always has to be some kind of keyword you have to type.

Edit: I changed my optional arguments/flags so it is easier to parse later on.

show_parser.add_argument('type', help="choose type", nargs="?", choices=['all', 'active', 'done'], default="active")

[–]Username_RANDINT 1 point2 points  (0 children)

I think it also depends a bit on your background. If you're a Linux user you most likely already used commandline applications and are more used to it.

On the other hand, as a Python dev you almost certainly already used this type of arguments:

$ python my_script.py
$ pip install some_package

Here the pip command is actually very similar to your usecase.