argparse

Option accepted with and without value

℡╲_俬逩灬. 提交于 2019-12-04 04:18:13
问题 I have a small script and I need it to be able to accept parameter with value and withou value. ./cha.py --pretty-xml ./cha.py --pretty-xml=5 I have this. parser.add_argument('--pretty-xml', nargs='?', dest='xml_space', default=4) But when I use --pretty-xml in xml_space will be 'none'. If I dont write this parameter in xml_space is stored the default value. I would need the exact opposite. 回答1: Leave out the default parameter and use a custom Action instead: class PrettyXMLAction(argparse

Python argparse : how to detect duplicated optional argument?

感情迁移 提交于 2019-12-04 03:43:57
问题 I'm using argparse with optional parameter, but I want to avoid having something like this : script.py -a 1 -b -a 2 Here we have twice the optional parameter 'a', and only the second parameter is returned. I want either to get both values or get an error message. How should I define the argument ? [Edit] This is the code: import argparse parser = argparse.ArgumentParser() parser.add_argument('-a', dest='alpha', action='store', nargs='?') parser.add_argument('-b', dest='beta', action='store',

Add top level argparse arguments after subparser args

我的未来我决定 提交于 2019-12-04 03:24:00
问题 How can you allow for top-level program arguments to be added after using a subcommand from a subparser? I have a program that includes several subparsers to allow for subcommands, changing the behavior of the program. Here is an example of how its set up: #!/usr/bin/env python # -*- coding: utf-8 -*- import argparse def task_a(): print('did task_a') def task_c(): print('did task_c') def task_d(): print('did task_d') def run_foo(args): a_arg = args.a c_arg = args.c if a_arg: task_a() if c_arg

Python's argparse: How to use keyword as argument's name

一世执手 提交于 2019-12-04 03:16:55
问题 lambda has a keyword function in Python: f = lambda x: x**2 + 2*x - 5 What if I want to use it as a variable name? Is there an escape sequence or another way? You may ask why I don't use another name. This is because I'd like to use argparse: parser = argparse.ArgumentParser("Calculate something with a quantity commonly called lambda.") parser.add_argument("-l","--lambda",help="Defines the quantity called lambda", type=float) args = parser.parse_args() print args.lambda # syntax error! Script

Using mutually exclusive between groups

拈花ヽ惹草 提交于 2019-12-04 03:01:30
I'm trying to have a mutually exclusive group between different groups: I have the arguments -a,-b,-c, and I want to have a conflict with -a and -b together, or -a and -c together. The help should show something like [-a | ([-b] [-c])]. The following code does not seem to do have mutually exclusive options: import argparse parser = argparse.ArgumentParser(description='My desc') main_group = parser.add_mutually_exclusive_group() mysub_group = main_group.add_argument_group() main_group.add_argument("-a", dest='a', action='store_true', default=False, help='a help') mysub_group.add_argument("-b",

argparse Python modules in cli

核能气质少年 提交于 2019-12-04 02:26:16
I am trying to run a python script from the Linux SSH Secure Shell command line environment, and I am trying to import the argparse library, but it gives the error: "ImportError: No module named argparse". I think that this is because the Python environment that the Linux shell is using does not have the argparse library in it, and I think I can fix it fix it if I can find the directories for the libraries being used by the Python environment, and copy the argparse library into it, but I can not find where that directory is located. I would appreciate any help on finding this directory (I

python argparse with dependencies

只谈情不闲聊 提交于 2019-12-04 02:22:11
I'm writing a script which has 2 arguments which are mutually exclusive, and an option that only makes sense with one of those arguments. I'm trying to set up argparse to fail if you call it with the argument that makes no sense. To be clear: -m -f makes sense -s makes sense -s -f should throw errors no arguments are fine. The code I have is: parser = argparse.ArgumentParser(description='Lookup servers by ip address from host file') parser.add_argument('host', nargs=1, help="ip address to lookup") main_group = parser.add_mutually_exclusive_group() mysql_group = main_group.add_argument_group()

How to have sub-parser arguments in separate namespace with argparse?

一个人想着一个人 提交于 2019-12-04 00:44:01
问题 I have the following test-code import argparse parser = argparse.ArgumentParser() parser.add_argument("--verbose", default = 0, type=int) subparsers = parser.add_subparsers(dest = "parser_name") parser_lan = subparsers.add_parser('car') parser_lan.add_argument("--boo") parser_lan.add_argument("--foo") parser_serial = subparsers.add_parser('bus') parser_serial.add_argument("--fun") print parser.parse_args() which defines two sub-parsers, having a different set of arguments. When I call the

Disable/Remove argument in argparse

蹲街弑〆低调 提交于 2019-12-04 00:05:53
问题 Is it possible to remove or disable an argument in argparse, such that it does not show in the help? How? It is easy to add new arguments: parser = argparse.ArgumentParser() parser.add_argument('--arg1', help='Argument 1') parser.add_argument('--arg2', help='A second one') And I know you can override arguments with a new definition by specifying the "resolve" conflict handler: #In one script that should stand-alone and include arg1: parser = argparse.ArgumentParser(conflict_handler='resolve')

argparse: some mutually exclusive arguments in required group

时光毁灭记忆、已成空白 提交于 2019-12-03 23:46:50
I have a set of arguments that can logically be separated in 2 groups: Actions: A1 , A2 , A3 , etc. Informations: I1 , I2 , I3 , etc. At least one of these arguments is required for the program to start, but "information" args can be used with "action" args. So At least one in Actions or Informations is required All Actions are mutually exclusive I can't find how to do it using argparse. I know about add_mutually_exclusive_group and its required argument, but I can't use it on "Actions" because it's not actually required. Of course, I could add a condition after argparse to manually check my