1

I want to create a command parser mycommand, using argparse, with two subcommands read and write: read should have just one argument which is some path, and write should have two arguments one of which is a path and the other a value. It should be possible to execute the command in the following way:

mycommand read <path>

mycommand write <path> <value>

without using labels for the <path>, <value> arguments, i.e. without requiring --path. How can I do this?

1 Answer 1

3

This is pretty straight forward following the docs:

import argparse

parser = argparse.ArgumentParser()

subparsers = parser.add_subparsers()

read = subparsers.add_parser('read')
read.add_argument('path')

write = subparsers.add_parser('write')
write.add_argument('path')
write.add_argument('value')

print(parser.parse_args(['read', 'foo']))
print(parser.parse_args(['write', 'foo', 'bar']))

Note that this doesn't tell you what parser parsed the arguments. If you want that, you can simply add a dest to the add_subparsers command:

subparsers = parser.add_subparsers(dest='subparser')

Finally, you can add a default attribute for each subparser that you can use to perform the actions specific to that subparser. This is spelled out in the docs too, but for completeness, in our example, it might look something like this1:

import argparse

parser = argparse.ArgumentParser()

subparsers = parser.add_subparsers(dest='subparser')

def handle_read(args):
    print('Handling read')
    print(args)
read = subparsers.add_parser('read')
read.add_argument('path')
read.set_defaults(handler=handle_read)

def handle_write(args):
    print('Handling write')
    print(args)
write = subparsers.add_parser('write')
write.add_argument('path')
write.add_argument('value')
write.set_defaults(handler=handle_write)

args = parser.parse_args()
args.handler(args)

1I added the dest to subparsers in this example too for illustrative purposes -- Using argparse with handler functions probably makes that attribute on args obsolete.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.