optparse

How to parse an argument without a name with Ruby's optparse

梦想与她 提交于 2019-12-03 23:29:26
I need to parse a command line like script.rb <mandatory filename> [options] with optparse . Sure I can write some custom code to handle the filename, then pass ARGV to optparse, but maybe there's a simpler way to do it? EDIT: there's another hacky way to parse such a command line, and that is pass ['--mandatory-filename'] + ARGV to optparse, then handle the --mandatory-filename option. First parse! with optparse, then scan the ARGV and raise if ARGV is empty. Like so: op.parse! filename = ARGV.pop raise "Need to specify a file to process" unless filename The mandatory filename will not be

Most pythonic way of accepting arguments using optparse

﹥>﹥吖頭↗ 提交于 2019-12-03 16:33:44
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 capabilities. I am only running 2.6, so argparse is not an option. The tutorials I have seen either use argparse,

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

房东的猫 提交于 2019-12-03 11:11:44
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 each part processed theirs. The use cases are: In a loosely coupled front-end program, where various

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

百般思念 提交于 2019-12-03 11:08:46
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. I believe optparse does not support what you require (not directly -- as you noticed, you can do it if you're willing to do all the extra work of a callback!-). You could also do it

How to comply to PEP 257 docstrings when using Python's optparse module?

五迷三道 提交于 2019-12-03 08:08:37
According to PEP 257 the docstring of command line script should be its usage message. The docstring of a script (a stand-alone program) should be usable as its "usage" message, printed when the script is invoked with incorrect or missing arguments (or perhaps with a "-h" option, for "help"). Such a docstring should document the script's function and command line syntax, environment variables, and files. Usage messages can be fairly elaborate (several screens full) and should be sufficient for a new user to use the command properly, as well as a complete quick reference to all options and

How do I format positional argument help using Python's optparse?

此生再无相见时 提交于 2019-12-03 06:44:20
问题 As mentioned in the docs the optparse.OptionParser uses an IndentedHelpFormatter to output the formatted option help, for which which I found some API documentation. I want to display a similarly formatted help text for the required, positional arguments in the usage text. Is there an adapter or a simple usage pattern that can be used for similar positional argument formatting? Clarification Preferably only using the stdlib. Optparse does great except for this one formatting nuance, which I

How do I format positional argument help using Python's optparse?

你离开我真会死。 提交于 2019-12-02 20:23:35
As mentioned in the docs the optparse.OptionParser uses an IndentedHelpFormatter to output the formatted option help, for which which I found some API documentation . I want to display a similarly formatted help text for the required, positional arguments in the usage text. Is there an adapter or a simple usage pattern that can be used for similar positional argument formatting? Clarification Preferably only using the stdlib. Optparse does great except for this one formatting nuance, which I feel like we should be able to fix without importing whole other packages. :-) The best bet would be to

python optparse, how to include additional info in usage output?

核能气质少年 提交于 2019-12-02 18:04:05
Using python's optparse module I would like to add extra example lines below the regular usage output. My current help_print() output looks like this: usage: check_dell.py [options] options: -h, --help show this help message and exit -s, --storage checks virtual and physical disks -c, --chassis checks specified chassis components I would like it to include usage examples for the less *nix literate users at my work. Something like this: usage: check_dell.py [options] options: -h, --help show this help message and exit -s, --storage checks virtual and physical disks -c, --chassis checks

creating an array from a command line option (python::optparse)

给你一囗甜甜゛ 提交于 2019-12-01 21:41:04
问题 There is a python script which reads a benchmark name from command line like this: -b benchname1 The code for this perpose is: import optparse import Mybench parser = optparse.OptionParser() # Benchmark options parser.add_option("-b", "--benchmark", default="", help="The benchmark to be loaded.") if options.benchmark == 'benchname1': process = Mybench.b1 elif options.benchmark == 'benchname2': process = Mybench.b2 else: print "no such benchmark!" what I want to do is to create a an array of

how to distribute a ruby script via homebrew

喜你入骨 提交于 2019-12-01 12:11:14
How can I deploy a simple ruby script via homebrew? Here's what I tried Wrote formula in a GitHub repo named homebrew-foo # file https://github.com/foo/homebrew-foo/blob/master/foo.rb class Foo < Formula desc "A command line tool" url "https://github.com/foo/foo/archive/master.zip" version "5.0.1" def install bin.install "foo" lib.install Dir["lib/*"] end end The other repository contains the ruby script. These are the files ./foo ./lib/libfile1.rb here's what the script does #!/usr/bin/env ruby require './lib/libfile1.rb' puts "came here" The problem is that the require fails. $ brew install