optparse

Can OptionParser skip unknown options, to be processed later in a Ruby program?

与世无争的帅哥 提交于 2019-12-21 03:42:46
问题 Is there any way to kick off OptionParser several times in one Ruby program, each with different sets of options? For example: $ myscript.rb --subsys1opt a --subsys2opt b Here, myscript.rb would use subsys1 and subsys2, delegating their options handling logic to them, possibly in a sequence where 'a' is processed first, followed by 'b' in separate OptionParser object; each time picking options only relevant for that context. A final phase could check that there is nothing unknown left after

Using ruby's OptionParser to parse sub-commands

人走茶凉 提交于 2019-12-20 08:41:15
问题 I'd like to be able to use ruby's OptionParser to parse sub-commands of the form COMMAND [GLOBAL FLAGS] [SUB-COMMAND [SUB-COMMAND FLAGS]] like: git branch -a gem list foo I know I could switch to a different option parser library (like Trollop), but I'm interested in learning how to do this from within OptionParser, since I'd like to learn the library better. Any tips? 回答1: Figured it out. I need to use OptionParser#order! . It will parse all the options from the start of ARGV until it finds

Python argparse argument with quotes

柔情痞子 提交于 2019-12-19 05:53:34
问题 Is there any way I can tell argparse to not eat quotation marks? For example, When I give an argument with quotes, argparse only takes what's inside of the quotes as the argument. I want to capture the quotation marks as well (without having to escape them on the command line.) pbsnodes -x | xmlparse -t "interactive-00" produces interactive-00 I want "interactive-00" 回答1: I think it is the shell that eats them, so python will actually never see them. Escaping them on the command line may be

Python argparse ignore unrecognised arguments

早过忘川 提交于 2019-12-17 07:12:53
问题 Optparse, the old version just ignores all unrecognised arguments and carries on. In most situations, this isn't ideal and was changed in argparse. But there are a few situations where you want to ignore any unrecognised arguments and parse the ones you've specified. For example: parser = argparse.ArgumentParser() parser.add_argument('--foo', dest="foo") parser.parse_args() $python myscript.py --foo 1 --bar 2 error: unrecognized arguments: --bar Is there anyway to overwrite this? 回答1: Replace

Mock command line arguments for Python script with `optparse`?

人盡茶涼 提交于 2019-12-12 13:34:01
问题 A Python script that I want to use (called snakefood) is normally run from the commandline and takes commandline arguments, eg: sfood /path/to/my/project The parsing of the commandline arguments happens in a file called gendeps.py using optparse . However, I want to use the snakefood module from another script. Is there a way I can somehow mock the passing of commandline arguments to snakefood or a way of rewriting gendeps.py so that it doesn't depend on optparse anymore? 回答1: You can always

Most pythonic way of accepting arguments using optparse

孤街醉人 提交于 2019-12-12 08:15:08
问题 I currently have a python file that utilizes sys.argv[1] to accept a string at the command line. It then performs operations on that string and then returns the modified string to the command line. I would like to implement a batch mode option in which I can provide a file of strings (one per line, fwiw) and have it return to the command line so that I can redirect the output doing something like $ python script.py -someflag file.txt > modified.txt while still retaining the current

Triggering callback on default value in optparse

自闭症网瘾萝莉.ら 提交于 2019-12-12 05:17:24
问题 I'm using Python's optparse to do what it does best, but I can't figure out how to make the option callback trigger on the default argument value if no other is specified via command-line; is this even possible? This would make my code much cleaner. I can't use argparse unfortunately, as the platform I'm running on has an outdated Python version. Edit: To provide more detail, I'm adding an option with a callback and a default value parser.add_option( "-f", "--format", type = "string", action

Set a default choice for optionparser when the option is given

一个人想着一个人 提交于 2019-12-11 06:24:54
问题 I have a python option parsers that parses an optional --list-something option. I also want the --list-something option to have an optional argument (an option) Using the argument default="simple" does not work here, otherwise simple will always be the default, not only when --list-something was given. from optparse import OptionParser, OptionGroup parser = OptionParser() options = OptionGroup(parser, "options") options.add_option("--list-something", type="choice", choices=["simple",

With Python's optparse module, how do you create an option that takes a variable number of arguments?

≯℡__Kan透↙ 提交于 2019-12-09 08:38:33
问题 With Perl's Getopt::Long you can easily define command-line options that take a variable number of arguments: foo.pl --files a.txt --verbose foo.pl --files a.txt b.txt c.txt --verbose Is there a way to do this directly with Python's optparse module? As far as I can tell, the nargs option attribute can be used to specify a fixed number of option arguments, and I have not seen other alternatives in the documentation. 回答1: I believe optparse does not support what you require (not directly -- as

displaying newlines in the help message when using python's optparse

北慕城南 提交于 2019-12-08 21:57:26
问题 I'm using the optparse module for option/argument parsing. For backwards compatibility reasons, I can't use the argparse module. How can I format my epilog message so that newlines are preserved? In the below example, I'd like the epilog to be printed as formatted. epi = \ """ Examples usages: Do something %prog -a -b foo Do something else %prog -d -f -h bar """ parser = optparse.OptionParser(epilog=epi) 回答1: See the first answer at: python optparse, how to include additional info in usage