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
24
votes
1 answer

Python argparse, provide different arguments based on parent argument value

here is what i would like to do : A command that looks like git command behavior. You don't get the same options whether you typed git commit or git checkout. But in my case i want to provide different arguments based on an argument value (a file…
Narthe
  • 492
  • 1
  • 4
  • 16
16
votes
2 answers

Is it possible to create subparsers in a django management command?

Title really says it all, but I currently have this, but it doesn't work: class Command(BaseCommand): help = ("Functions related to downloading, parsing, and indexing the " "content") def add_arguments(self, parser): …
mlissner
  • 17,359
  • 18
  • 106
  • 169
11
votes
2 answers

How to handle CLI subcommands with argparse

I need to implement a command line interface in which the program accepts subcommands. For example, if the program is called “foo”, the CLI would look like foo cmd1 foo cmd2 foo cmd3 cmd1 and cmd3 must be used with at…
Enea
  • 113
  • 1
  • 4
6
votes
1 answer

How do I check for a particular subparser?

How do I check for a particular subparser? import argparse if __name__ == "__main__": mainparser = argparse.ArgumentParser() submainadder = mainparser.add_subparsers(title='subcommands') parser_ut = submainadder.add_parser('unittest') …
n611x007
  • 8,952
  • 8
  • 59
  • 102
4
votes
2 answers

argparse not handling abbreviations in subparser properly

(Run on python 3.6.0) TL;DR Usage: prog.py {caesar | vigenere} [key] parser = argparse.ArgumentParser() subp = parser.add_subparsers() caesar = subp.add_parser("caesar", aliases=["c"], allow_abbrev=True) args = parser.parse_args() $ python prog.py…
cdpp
  • 152
  • 2
  • 9
3
votes
1 answer

Display subgroups of commands with argparse subparser

I am currently developing a program in Python that contains ~40 sub commands. The parser is done using argparse. As the number of sub commands increases it is becoming complicated to search for the command of interest. Currently, it appears as…
dputhier
  • 734
  • 1
  • 7
  • 23
2
votes
0 answers

argparse: How to specify custom help text for subparsers?

Using argparse under python3, I'm trying to get subparser help text and sub-subparser help text to be set in customized ways. The following example program illustrates my issue (the example program's source code appears at the bottom of this…
HippoMan
  • 2,119
  • 2
  • 25
  • 48
2
votes
0 answers

How to declare an optional argument to the parser which disables compulsory argument?

The title may be confusing but I can't think of a better explanation. Basically, I have a program which operates on some input file with a bunch of optional arguments. The input file is compulsory for my program. So I wrote a parser like…
Asocia
  • 5,935
  • 2
  • 21
  • 46
2
votes
2 answers

Argparse: Optional argument with choices that have variable arguments

I have a CLI that I'm trying to improve. What I would like to do is have an optional argument with 3 choices, and depending on your choice you are required to enter certain arguments to that choice. For example: --create dog DOG_NAME…
dzzl
  • 105
  • 1
  • 1
  • 11
2
votes
0 answers

Subparser function calls

How do I call the following functions below based on user input? For example, if they type: python test.py cmd1 -n hostname it will call def cmd1 and pass the -n parameter (hostname) to function import argparse def cmd1(node): …
python_newbie
  • 111
  • 1
  • 9
2
votes
1 answer

Argparser with default value and required

I try to build an argparser, where one of the parsers should have a default value, and is also a required. I have the following so far: fulllist_parser.add_argument( '--type', required=True, default="VirtualMachine", type=str, …
derchris
  • 285
  • 4
  • 11
1
vote
1 answer

How to make optional subparser in python3?

I want to input args that not configured in argparse: parser = argparse.ArgumentParser(prog='PROG') subparsers = parser.add_subparsers(help='sub-command help', dest="character", required=False) subparsers.required = False base_subparser =…
JAY
  • 13
  • 2
1
vote
0 answers

argparse add subparser that starts with a dash

I'm trying to create a Pacman wrapper in Python. I'm having trouble to parse the arguments in the same way Pacman does. (Described at https://man.archlinux.org/man/pacman.8) In order to parse the arguments, I need to create a sub parser that starts…
simonzack
  • 19,729
  • 13
  • 73
  • 118
1
vote
1 answer

Argparse outputting help text twice

After an hour googling, I can't find anybody who has had anything resembling this issue besides myself. I created a command line interface with argparse. Originally I had tried to leverage argparse's built in help text behavior. But my boss isn't…
1
vote
0 answers

python argparse having multiple subparsers in one commandline

I am trying to parse multiple groups of arguments in one command line. both setup1 and setup2 can have identical arguments python3 setup1 --param1 1 --param2 0 setup2 --param1 0 --param2 -1 I tried to use the subparse as below, but it appears it…
brane
  • 585
  • 6
  • 20
1
2 3