optparse

SystemExit: 2 error when calling parse_args() in iPython Notebook

ⅰ亾dé卋堺 提交于 2019-12-08 20:16:32
问题 I'm learning to use Python and scikit-learn and executed the following block of codes (originally from http://scikit-learn.org/stable/auto_examples/document_classification_20newsgroups.html#example-document-classification-20newsgroups-py) in iPython notebook (using Python 2.7): from __future__ import print_function from optparse import OptionParser # parse commandline arguments op = OptionParser() op.add_option("--report", action="store_true", dest="print_report", help="Print a detailed

how to access nargs of optparse-add_action?

末鹿安然 提交于 2019-12-08 04:34:17
问题 I am working on one requirement for my project using command line utility:optparse. Suppose if I am using add_option utility like below: parser.add_option('-c','--categories', dest='Categories', nargs=4 ) I wanted to add check for -c option if user does not input 4 arguments. something like this: if options.Categories is None: for loop_iterate on nargs: options.Categories[loop_iterate] = raw_input('Enter Input') How to access nargs of add_option().? PS:I do not want to have check using print

Ruby optparse Limitations

[亡魂溺海] 提交于 2019-12-07 01:16:49
问题 I currently script in Python but I wish to try Ruby for several reasons. I've looked at a lot of sample code and read a lot of documentation over the last week. One point of concern I have is the lack of a proper command line argument parsing libraries in Ruby. Ruby experts, don't get mad at me — maybe I don't know. That's why I am here. In Python, I was used to using argparse which in my opinion is simply perfect (maybe for my needs). Unfortunately though, OptionParser doesn't allow for the

OptionParser returning bool instead of argument?

孤人 提交于 2019-12-06 17:02:49
问题 When I run this sample from the OptionParser documentation: require 'optparse' options = {} OptionParser.new do |opts| opts.banner = "Usage: example.rb [options]" opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| options[:verbose] = v end end.parse! p options p ARGV and type: ruby test.rb -v 100 , it returns: {:verbose=>true} ["100"] Shouldn't verbose be 100 , not a boolean? I have no idea about this, does anyone have any advice? 回答1: You've specified that the -v option does not have an

Parsing empty options in Python

∥☆過路亽.° 提交于 2019-12-06 06:04:54
问题 I have an application that allows you to send event data to a custom script. You simply lay out the command line arguments and assign what event data goes with what argument. The problem is that there is no real flexibility here. Every option you map out is going to be used, but not every option will necessarily have data. So when the application builds the string to send to the script, some of the arguments are blank and python's OptionParser errors out with "error: --someargument option

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

旧巷老猫 提交于 2019-12-05 08:35:18
问题 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. 回答1: First parse! with optparse, then scan the ARGV and raise if ARGV is empty. Like so: op.parse! filename

Ruby optparse Limitations

匆匆过客 提交于 2019-12-05 05:37:48
I currently script in Python but I wish to try Ruby for several reasons. I've looked at a lot of sample code and read a lot of documentation over the last week. One point of concern I have is the lack of a proper command line argument parsing libraries in Ruby. Ruby experts, don't get mad at me — maybe I don't know. That's why I am here. In Python, I was used to using argparse which in my opinion is simply perfect (maybe for my needs). Unfortunately though, OptionParser doesn't allow for the flexibility and features that argparse does. I am specifically looking at the following constraints for

OptionParser returning bool instead of argument?

时光毁灭记忆、已成空白 提交于 2019-12-04 22:36:32
When I run this sample from the OptionParser documentation: require 'optparse' options = {} OptionParser.new do |opts| opts.banner = "Usage: example.rb [options]" opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| options[:verbose] = v end end.parse! p options p ARGV and type: ruby test.rb -v 100 , it returns: {:verbose=>true} ["100"] Shouldn't verbose be 100 , not a boolean? I have no idea about this, does anyone have any advice? You've specified that the -v option does not have an argument: opts.on("-v", ... If you want it to take an argument then you have to say so: opts.on("-v n", "-

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

早过忘川 提交于 2019-12-04 12:57:08
问题 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

Parsing empty options in Python

梦想与她 提交于 2019-12-04 09:41:22
I have an application that allows you to send event data to a custom script. You simply lay out the command line arguments and assign what event data goes with what argument. The problem is that there is no real flexibility here. Every option you map out is going to be used, but not every option will necessarily have data. So when the application builds the string to send to the script, some of the arguments are blank and python's OptionParser errors out with "error: --someargument option requires an argument" Being that there are over 200 points of data, it's not like I can write separate