subcommand

Parse multiple subcommands in python simultaneously or other way to group parsed arguments

柔情痞子 提交于 2020-01-02 19:51:14
问题 I am converting Bash shell installer utility to Python 2.7 and need to implement complex CLI so I am able to parse tens of parameters (potentially up to ~150). These are names of Puppet class variables in addition to a dozen of generic deployment options, which where available in shell version. However after I have started to add more variables I faced are several challenges: 1. I need to group parameters into separate dictionaries so deployment options are separated from Puppet variables. If

Parse multiple subcommands in python simultaneously or other way to group parsed arguments

风流意气都作罢 提交于 2020-01-02 19:51:11
问题 I am converting Bash shell installer utility to Python 2.7 and need to implement complex CLI so I am able to parse tens of parameters (potentially up to ~150). These are names of Puppet class variables in addition to a dozen of generic deployment options, which where available in shell version. However after I have started to add more variables I faced are several challenges: 1. I need to group parameters into separate dictionaries so deployment options are separated from Puppet variables. If

Multiple invocation of the same subcommand in a single command line

老子叫甜甜 提交于 2019-12-18 08:54:07
问题 I'm trying to figure out how to use argparser to do the following: $ python test.py executeBuild --name foobar1 executeBuild --name foobar2 .... getBuild itself is a sub-command. My goal is to have the script have the capability to chain a series of sub-command ( executeBuild being one of them) and execute them in order. In the example above, it would execute a build, then setup the environment, then execute build again. How can I accomplish this with argparse? I've tried the following: main

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'

Parse multiple subcommands in python simultaneously or other way to group parsed arguments

一世执手 提交于 2019-12-06 11:13:00
I am converting Bash shell installer utility to Python 2.7 and need to implement complex CLI so I am able to parse tens of parameters (potentially up to ~150). These are names of Puppet class variables in addition to a dozen of generic deployment options, which where available in shell version. However after I have started to add more variables I faced are several challenges: 1. I need to group parameters into separate dictionaries so deployment options are separated from Puppet variables. If they are thrown into the same bucket, then I will have to write some logic to sort them, potentially

Python argparse positional arguments and sub-commands

拈花ヽ惹草 提交于 2019-11-30 18:16:31
I'm working with argparse and am trying to mix sub-commands and positional arguments, and the following issue came up. This code runs fine: import argparse parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() parser.add_argument('positional') subparsers.add_parser('subpositional') parser.parse_args('subpositional positional'.split()) The above code parses the args into Namespace(positional='positional') , however when I change the positional argument to have nargs='?' as such: import argparse parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() parser.add

Python argparse positional arguments and sub-commands

混江龙づ霸主 提交于 2019-11-30 02:23:50
问题 I'm working with argparse and am trying to mix sub-commands and positional arguments, and the following issue came up. This code runs fine: import argparse parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() parser.add_argument('positional') subparsers.add_parser('subpositional') parser.parse_args('subpositional positional'.split()) The above code parses the args into Namespace(positional='positional') , however when I change the positional argument to have nargs='?' as

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

argparse optional subparser (for --version)

穿精又带淫゛_ 提交于 2019-11-28 07:32:08
I have the following code (using Python 2.7): # shared command line options, like --version or --verbose parser_shared = argparse.ArgumentParser(add_help=False) parser_shared.add_argument('--version', action='store_true') # the main parser, inherits from `parser_shared` parser = argparse.ArgumentParser(description='main', parents=[parser_shared]) # several subcommands, which can't inherit from the main parser, since # it would expect subcommands ad infinitum subparsers = parser.add_subparsers('db', parents=[parser_shared]) ... args = parser.parse_args() Now I would like to be able to call this

How to handle CLI subcommands with argparse

和自甴很熟 提交于 2019-11-27 06:14:42
问题 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