Questions tagged [optionparser]

93 questions
4
votes
2 answers

multiple invocations of OptionParser.parse_args() within the same python execution

I have the following example setup: |-- main_script.py `-- module |-- __init__.py `-- submodule.py where the contents of main_script are: import optparse import module parser = optparse.OptionParser() group =…
Ali-Akber Saifee
  • 4,406
  • 1
  • 16
  • 18
4
votes
2 answers

Ruby: OptionParser: String Arg & Hash Assignment

Using OptionParser for string argument input and hash assignment. What is the best way to read-in multiple variables for a single argument? How do I then assign those to a hash to reference? Here is what I have so far: large_skus =…
3
votes
2 answers

Ruby OptionParser not parsing -- commands properly

Here is a stripped down version of OptionParser OptionParser.new do |opts| opts.on('-f', '--format FORMAT', 'output format (text, html, yml, json, xml)') do |format| options['format'] = format end end Here is the trial…
TIMBERings
  • 688
  • 1
  • 8
  • 28
3
votes
1 answer

Informational text for rails runners

I have a script like OptionParser.new do |opts| opt.on("-h","--help","help") do puts opts end end.parse! But whenever I call rails runner my_script.rb --help it shows me help for the rails runner and not my script. Is there a way that I can…
Xodarap
  • 11,581
  • 11
  • 56
  • 94
3
votes
2 answers

optionparser pass an array to ruby file

I have a trouble with passing an arguments to my ruby file. IE, OptionParser.new do |opts| opts.banner = 'Usage: mp3split.rb [options]' opts.on('-f', '--filename fName1,fName2,fName3', Array, 'absolute or relative pathes to file') { |f|…
Alex Antonov
  • 14,134
  • 7
  • 65
  • 142
3
votes
2 answers

How to string format OptionParser() help message?

How to string format OptionParser() help message? It seems to ignore the new line character? Please see below code. parser = OptionParser() parser.add_option("--s", dest="s", type="string", help="first line \n second line") Intention: current…
user3388884
  • 4,748
  • 9
  • 25
  • 34
3
votes
1 answer

Force mandatory command line argument using OptionParse in Ruby

I have this code: options = {} opt_parse = OptionParser.new do |opts| opts.banner = "Usage: example.rb [options]" opts.on("-g", "--grade [N]", "Grade") do |g| options[:grade] = g end opts.on_tail("-h", "--help", "Show this message")…
Israel
  • 3,252
  • 4
  • 36
  • 54
2
votes
4 answers

Why does this command fail when I use a # in command line args?

I have the following command: ruby SaveAllDatabases.rb 192.168.0.15 1024 -r #0-D --non-interactive It's a fairly basic command in which I run a ruby script with some command line arguments. The -r argument is a regular expression (#0-D). If I run…
Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
2
votes
2 answers

unittest for command line arguments

How do I unit test my script for incorrect command line arguments? For example, my_script.py -t should give an error since -t flag is not present, as shown in the code below: parser = OptionParser() parser.add_option("-d", …
Steve
  • 101
  • 1
  • 7
2
votes
3 answers

python script envoke -h or --help if no options are chosen

Trying to make my script more generic so I added some flags. My problem is the help only works if you type -h , obviously. I want to envoke -h when no flags are selected. For example: python 0_log_cleaner.py Traceback (most recent call last): …
chowpay
  • 1,515
  • 6
  • 22
  • 44
2
votes
0 answers

Providing an argument that looks like an option (rails runner / OptionParser / optparse)

I have a script magic which uses rails runner to invoke another script magic-foo.rb, which does it's own option parsing. Something like this: exec("#rails", "runner", "#{some_path}/bin/magic-foo.rb", *arguments) I want to expose the --help option…
Josh Barr
  • 21
  • 3
2
votes
2 answers

Can I use Ruby's OptionParser to accept an arbitrary argument pattern?

Let's say that I have a simple Ruby app where I want the first argument (if any) to specify the environment: TEST, DEVELOPMENT or PRODUCTION (with DEVELOPMENT being the default if no argument is given). For instance, ruby myapp.rb test would run it…
Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
2
votes
2 answers

Application with both console and gui mode

I have a python console app. Like most python console apps it uses the OptionParser module to take arguments. I've now developed a GUI for my app using wxPython and i'd like to integrate the two. I'd like my app to be run both from the console and…
2
votes
1 answer

How do you use OptionParser to open a file with command-line options?

I'm getting this error when trying to print line from a file via command-line options no implicit conversion of true into String (TypeError) from threat_detection.rb:64:in 'new' from threat_detection.rb:64:in '
' If I use the file name…
insecure-IT
  • 2,068
  • 4
  • 18
  • 26
2
votes
3 answers

Modify Ruby OptionParser error message

I'm learning to use Ruby's OptionParser class. How can I improve the quality of the parsers error messages? Here is an example of an flag with a mandatory option that must be one of hour, day, week, or month. opt_parser = OptionParser.new do |opts| …
everett1992
  • 2,351
  • 3
  • 27
  • 38