argparse

Best way to make argument parser accept absolute number and percentage?

試著忘記壹切 提交于 2019-12-13 22:33:26
问题 I am trying to write a Nagios style check to use with Nagios. I have working script that takes in something like -w 15 -c 10 and interprets that as "Warning at 15%, Critical at 10%". But I just realized that in the built-in Nagios plugins, the same arguments would mean "Warning at 15MB, Critical at 10MB"; I would instead need to enter -w 15% -c 10% to get the above behavior. So my question is, what is the best way to make my script behave like the built-in Nagios scripts? The only way I can

How can I define global options with sub-parsers in python argparse?

做~自己de王妃 提交于 2019-12-13 14:25:52
问题 I'm trying to figure out how to add global option in a sub-parser scenario with pythons arparse library. Right now my code looks like this: def parseArgs(self): parent_parser = argparse.ArgumentParser(add_help=False) parent_parser.add_argument('--debug', default=False, required=False, action='store_true', dest="debug", help='debug flag') main_parser = argparse.ArgumentParser() main_parser.add_argument('--debug', default=False, required=False, action='store_true', dest="debug", help='debug

Tensorflow ArgumentError Running CIFAR-10 example

爷,独闯天下 提交于 2019-12-13 13:06:16
问题 I am trying to run the CIFAR-10 example of Tensorflow. However when executing python cifar10.py I am getting the error attached below. I have installed Version 0.6.0 of the Tensorflow package using pip. The framework is working fine on other models including the MNIST tutorial and some self developed networks. Does anybody has an idea about the origin of the problem? Do you think I should open an issue on github? I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library

How to include one positional argument into argparse mutually exclusive group?

亡梦爱人 提交于 2019-12-13 12:07:30
问题 I know that does not make sense multiple positional arguments into a mutually exclusive group because you can't say who is who. But I need to include ONE positional argument into that. What I need: $ myprogram -h usage: myprogram [-h] [--delete value | --update value | value] Where positional value is the default action (kind of "--include"). ( myprogram without arguments must be valid too). My first attempt (this doesn't works): parser = ArgumentParser() group = parser.add_mutually_exclusive

Python argparse as a function

时光总嘲笑我的痴心妄想 提交于 2019-12-13 11:48:32
问题 Is there anything inherently wrong with getting command-line arguments in this way? I mean by putting the argument parsing into its own function. Would it be considered non-Pythonic or more so? #!/usr/bin/python import argparse def getArgs(argv=None): parser = argparse.ArgumentParser(description="calculate X to the power of Y") group = parser.add_mutually_exclusive_group() group.add_argument("-v", "--verbose", action="store_true") group.add_argument("-q", "--quiet", action="store_true")

Is there any way to instruct argparse (Python 2.7) to remove found arguments from sys.argv?

情到浓时终转凉″ 提交于 2019-12-13 11:36:22
问题 I'm fully midstream in the development process for what is turning into a fairly substantial Python 2.7 project. Right now I have all of my unittest classes lumped together in their own module, tests.py , which stands at about 3300 lines. This is crazy big, impossible to navigate, all around bad practice, etc. So, my current task is to refactor it into submodules. As part of this refactoring, I want to make it easy to just run a subset of the tests from the commandline. For example: $ python

looking for best way of giving command line arguments in python, where some params are req for some option and some params are req for other options

前提是你 提交于 2019-12-13 04:30:48
问题 Hi i am trying to send command line arguments for first time. The condition is that one parameter is required for one option and for others other parameter.(looking for user friendly). The below code looks need some optimization: import argparse parser = argparse.ArgumentParser(description='Usage options.') parser.add_argument('-o','--options',help='Options available: run,rerun,kill, resume, suspend',required=True) parser.add_argument('-c', '--config',help='config file input',type=file

Convert many argparse conditional arguements to a more pythonic solution

為{幸葍}努か 提交于 2019-12-13 04:26:28
问题 I am working on a cli application using python. I have a set of arguments that are mutually exclusive and a set of arguments that must be there if one of those mutually exclusive arguments is passed. I have got it working using brute force and lengthy if conditions, but I feel like there is a neat way to do this. Researching about that told me subparsers might be useful, however I am not able to correctly use subparsers at all. The conditions I want are the following- Main activities +-----+-

parser.add_argument and parser.parse_args() with jupyter

孤人 提交于 2019-12-13 04:18:49
问题 I'm trying to run some code with a jupyter notebook, but from the beginning, I have issues. Indeed, it looks like I can't use these commands: parser.add_argument('--lr', default=0.1, type=float, help='learning rate') parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint') usage: ipykernel_launcher.py [-h] [--lr LR] [--resume] and ipykernel_launcher.py gives error: unrecognized arguments: -f And: args = parser.parse_args() /Library/Frameworks/Python.framework

Have argparse collect, but not respond to, flags

时光毁灭记忆、已成空白 提交于 2019-12-13 01:29:48
问题 I have a script which takes in some arguments, uses some of those argument to choose a script to run, and passes the rest of the arguments to that script. So it looks something like this: parser = ArgumentParser() parser.add_argument('script', choices['a', 'b']) parser.add_argument('rest_args', nargs='*') args = parser.parse_args() if args.script == 'a': subprocess.call('python a.py %s' % ' '.join(args.rest_args)) else: subprocess.call('python b.py %s' % ' '.join(args.rest_args)) This works