Questions tagged [subparsers]

Use this tag for questions related to a subparser, an entity that supports the creation of sub-commands.

Many programs split up their functionality into a number of sub-commands, for example, the svn program can invoke sub-commands like svn checkout, svn update, and svn commit. Splitting up functionality this way can be a particularly good idea when a program performs several different functions which require different kinds of command-line arguments. supports the creation of such sub-commands.

Source: Subparsers

While most questions of this tag are related to Python, Django and C# are also welcomed.

45 questions
1
vote
1 answer

add_subparsers doesn't identify sub_argument

I'm trying to build a script that uses subparsers arguments. However, I can not pass any of the sub-arguments as a parameter. Resulting in "invalid choice:" for any input combination. Example input: python3 preprocess.py -d ../data/acm/ tf -l…
1
vote
1 answer

Argparse not recognizing arguments

I am having some trouble with argparse. My goal is to have the user select one and only one option (-a, -b, -c, etc.) and then the arguments for that option. I'm using subparsers to do this: parser_iq = subparsers.add_parser('iq', help='iq…
helloworld95
  • 366
  • 7
  • 17
1
vote
2 answers

How to create subparser with argparse from existing program in Python 3?

Original post: If one has an executable mini_program.py that uses argparse with the following structure: def main(): parser = argparse.ArgumentParser() parser.add_argument('-X', '--attribute_matrix', type=str, help = 'Input:…
O.rka
  • 29,847
  • 68
  • 194
  • 309
1
vote
1 answer

Python Argparse subparsers

I am using the Argparse Module to parse command line options. I have a main script which calls either subscript A or subscript B. I now have a a subparser instance for A and B and the main parser instance which should contain info about variables…
Torilla
  • 383
  • 5
  • 16
1
vote
1 answer

How can I add more subparsers to an argpare parser?

If my code calls a function which returns an ArgumentParser that already has some subparsers defined, how can I add more subparsers? I'd like to do something this: parser_with_subparsers = build_parser() additional_subparsers =…
David Board
  • 349
  • 3
  • 12
1
vote
2 answers

How to Set a Default Subparser using Argparse Module with Python 2.7

I'm using Python 2.7 and I'm trying to accomplish a shell like behavior using argparse. My issue, in general, that I cannot seem to find a way, in Python 2.7, to use argparse's subparsers as optional. It's kind of hard to explain my issue so I'll…
Yoavhayun
  • 499
  • 1
  • 8
  • 16
1
vote
1 answer

Python argparse override help from parent

I have a CLI I'm building which utilizes subparsers for sub-commands similar to tools like git. Some of my sub-commands share common options so I have a group parser that defines the options, and each sub-command that needs them uses…
iceblueorbitz
  • 920
  • 1
  • 7
  • 17
1
vote
0 answers

Why are arguments specific for one subcommand being required for my entire command line program to run?

I am building a command-line program using the argparse module, and have been trying to design two separate, mutually exclusive groups of arguments that perform completely different tasks. I decided to use separate the two sets of arguments by…
Bob McBobson
  • 743
  • 1
  • 9
  • 29
1
vote
2 answers

How to obtain argparse subparsers from a parent parser (to inspect defaults)

Suppose that I create a parser with a default value for an argument, and then give it a subparser with a further default value for an argument. In [1]: parser = argparse.ArgumentParser(description='test') In [2]: parser.add_argument("--test",…
ely
  • 74,674
  • 34
  • 147
  • 228
1
vote
2 answers

Python argparse: Get name of subparser program in help string

I'm writing an argument parser for a python module with various subparsers. My objective is to have an argument that's shared whose Argument constructor is passed to multiple children: from argparse import ArgumentParser parser =…
1
vote
2 answers

argparse subparser implied by other parameters

The usually way to define a subparser is to do master_parser = argparse.ArgumentParser() subparsers = master_parser.add_subparsers() parser = subparsers.add_parser('sub') parser.add_argument('--subopt') and the subparser would be called with…
user2283347
  • 670
  • 8
  • 12
1
vote
1 answer

Argparse: parse multiple subcommands

Did some research, but couldn't find any working solution. I'm trying to parse the following command line, where 'test' and 'train' are two independent subcommands each having distinct arguments: ./foo.py train -a 1 -b 2 ./foo.py test -a 3 -c…
memecs
  • 7,196
  • 7
  • 34
  • 49
1
vote
0 answers

Is it possible to name subparser (from Python argparse) like '--name' or even '-n'?

I have started work with argparse because I have to write Unix-style argument parsing for mailer script. With 'auto' option, which provides filename mailer, it will execute script received as filename, grabbing prepared mail and just sending it. In…
Izzy
  • 755
  • 2
  • 9
  • 17
0
votes
1 answer

How can I implement an argument parser with a variable number of positional arguments and optional subcommands in Python?

I am trying to implement an argument parser with the argparse module in Python. My goal is to allow a variable number of positional input arguments to the main parser and then optionally call a sub parser to perform some task. I'm interested in…
0
votes
1 answer

How to pass in undefined parameters when use subparsers

When I use the subparsers, the subparers are optional parameters, and I have to choose one of them. Now, I want to implement the ability to pass in a default option when an option parameter that is not defined in the subparse is passed in, for…
JAY
  • 13
  • 2