subparsers

Argparse optional arguments with multilevel parser/subparser

你说的曾经没有我的故事 提交于 2021-02-11 15:40:37
问题 I have a set of parsers and subparsers to build a production or development system. If the user picks production, he can add options and all is well. If he pics development, he can enter an architecture and then enter build options. This is where it gets sticky. I want him to be able to select build option 'comms' 'server' or 'all', but if he picks server, he has more choices. My implementation is below. I tried combinations of parsers and subpasers (it seems that arguments can only be added

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

不打扰是莪最后的温柔 提交于 2019-12-30 14:15:11
问题 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", dest="test", default="hello") Out[2]: _StoreAction(option_strings=['--test'], dest='test', nargs=None, const=None, default='hello', type=None, choices=None, help=None, metavar=None) In [3]: parser.get_default("test") Out[3]: 'hello' In [4]: subparsers =

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

巧了我就是萌 提交于 2019-12-29 07:19:12
问题 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 describe what I require from my program. The program has 2 modes of work: Starting the program with a given command (each command has it's own additional arguments) and additional arguments will run a specific task. Starting the program without a

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 03:55:41
问题 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: Path/to/Tab-separated-value.tsv') parser.add_argument('-y', '--target_vector', type=str, help = 'Input: Path/to/Tab-separated-value.tsv') opts = parser.parse_args() if __name__ == "__main__": main() How can one create a controller program parent_program.py that uses

Python argparse override help from parent

≡放荡痞女 提交于 2019-12-12 04:39:16
问题 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 parents=group_parser as one of the arguments. For example: group_parser = argparse.ArgumentParser() group_parser.add_argument('-f', '--foo', action='store_true') command1_parser = subparsers.add_parser('command1', parents=[group_parser]) command2_parser =

Argparse: parse multiple subcommands

守給你的承諾、 提交于 2019-12-11 05:05:56
问题 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 4 ./foo.py train -a 1 -b 2 test -a 3 -c 4 I've been trying using two subparsers ('test','train') but it seems like only one can be parsed at the time. Also it would be great to have those subparsers parents of the main parser such that, e.g. command '-a'

How do I check for a particular subparser?

好久不见. 提交于 2019-12-10 15:33:02
问题 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') stuff = mainparser.parse_args() # if 'unittest' was selected: # do_things() 回答1: Maybe something like this ? import argparse def do_things(args): print args # Do your stuff mainparser = argparse.ArgumentParser() submainadder = mainparser.add_subparsers(title=

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

前提是你 提交于 2019-12-01 14:47:31
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", dest="test", default="hello") Out[2]: _StoreAction(option_strings=['--test'], dest='test', nargs=None, const=None, default='hello', type=None, choices=None, help=None, metavar=None) In [3]: parser.get_default("test") Out[3]: 'hello' In [4]: subparsers = parser.add_subparsers(dest="command") In [5]: parser_other = subparsers.add_parser("other") In [6]:

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

安稳与你 提交于 2019-11-29 08:55:45
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 describe what I require from my program. The program has 2 modes of work: Starting the program with a given command (each command has it's own additional arguments) and additional arguments will run a specific task. Starting the program without a command will start a shell-like program that can take a line of arguments and process them as if the

How to handle CLI subcommands with argparse

╄→гoц情女王★ 提交于 2019-11-28 11:33:00
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 <cmd1-options> foo cmd2 foo cmd3 <cmd3-options> cmd1 and cmd3 must be used with at least one of their options and the three cmd* arguments are always exclusive. I am trying to use subparsers in argparse, but with no success for the moment. The problem is with cmd2 , that has no arguments: if I try to add the subparser entry with no arguments, the namespace returned by parse_args will not contain any information telling me that this