Questions tagged [optionparser]
93 questions
52
votes
8 answers
How do you specify a required switch (not argument) with Ruby OptionParser?
I'm writing a script and I want to require a --host switch with value, but if the --host switch isn't specified, I want the option parsing to fail.
I can't seem to figure out how to do that. The docs seem to only specify how to make the argument…

Teflon Ted
- 8,696
- 19
- 64
- 78
30
votes
4 answers
Using ruby's OptionParser to parse sub-commands
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),…

rampion
- 87,131
- 49
- 199
- 315
25
votes
2 answers
How to generate OptionParser require arguments
The code below works, but I am manually raising argument errors for the required arguments using fetch, when I want to build the required arguments into the native OptionParser sytax for required parameters:
# ocra script.rb -- --type=value
options…

lukemh
- 5,043
- 7
- 30
- 38
22
votes
3 answers
OptionParse with no arguments show banner
I'm using OptionParser with Ruby.
I other languages such as C, Python, etc., there are similar command-line parameters parsers and they often provide a way to show help message when no parameters are provided or parameters are wrong.
options =…

Israel
- 3,252
- 4
- 36
- 54
16
votes
4 answers
How do I handle a missing mandatory argument in Ruby OptionParser?
In OptionParser I can make an option mandatory, but if I leave out that value it will take the name of any following option as the value, screwing up the rest of the command line parsing.
Here is a test case that echoes the values of the options:
$…

Rob Jones
- 302
- 3
- 7
15
votes
3 answers
boost::program_options config file option with multiple tokens
I can not seem to be able to read from config file multitoken options like I can from command line. What is the syntax for the config file?
This is how the option description is added:
//parser.cpp
- - -
po::options_description* generic;
generic=new…

Regel
- 607
- 1
- 6
- 11
13
votes
2 answers
Is OptionParser in conflict with Sphinx?
I'm trying to write a documentation for my project in Sphinx and whenever Sphinx encounters OptionParser in my module it gives me:
sphinx-build: error: no such option: -b
I thought that it's impossible, so I wrote a simple module to check…

Michal
- 6,411
- 6
- 32
- 45
12
votes
3 answers
understanding OptionParser
I was trying out optparse and this is my initial script.
#!/usr/bin/env python
import os, sys
from optparse import OptionParser
parser = OptionParser()
usage = "usage: %prog [options] arg1 arg2"
parser.add_option("-d", "--dir", type="string",
…

MacUsers
- 2,091
- 3
- 35
- 56
8
votes
1 answer
How to use variable arguments with ruby's OptionParser
I don't know ruby very well, but I'm trying to add some functionality to this script a co-worker wrote.
Basically right now it takes a few flags and standard in as input, and it uses OptionParser to parse the flags.
I want to use OptionParser to…

icco
- 3,064
- 4
- 35
- 49
7
votes
5 answers
How do I get only long options work in OptionParser in Ruby?
I have such a simple code in Ruby (test.rb):
#! /usr/bin/env ruby
require 'optparse'
OptionParser.new do |option|
option.on("--sort", "Sort data") do
puts "--sort passed"
end
end.parse!
then I run it: ./test.rb -s and got:
--sort…

blackst0ne
- 681
- 6
- 22
6
votes
2 answers
Ruby OptionParser Short Code for Boolean Option?
I am using Ruby's OptionParser (require 'optparse') processing a "verbose" option that can be either true or false. It is in the code like this:
parser.on('-v', '--[no-]verbose', 'Verbose mode') do |v|
self.verbose = v
end
I support…

Keith Bennett
- 4,722
- 1
- 25
- 35
6
votes
2 answers
Parse multiple command line options in Ruby using OptionParser
I've just started using OptionParser for Ruby and I wanted to use flags that would use more than just one argument.
For instance, I would like to be able to run:
script --move src dst
Note how src and dst are not separated using a coma.
My initial…

Daniel Hernandez
- 635
- 1
- 11
- 22
6
votes
5 answers
OptionParser python module - multiple entries of same variable?
I'm writing a little python script to get stats from several servers or a single server, and I'm using OptionParser to parse the command line input.
#!/usr/bin/python
import sys
from optparse import OptionParser
...
parser.add_option("-s",…

jduncan
- 677
- 2
- 12
- 22
5
votes
1 answer
Extending Scopt OptionParser in Scala
I'm trying to have a base option parser with some default parameters.
In other projects i would like to extend the option parser with other parameters.
Something like:
case class Config(foo: String = null)
trait DefaultParser { self:…

Udy
- 2,492
- 4
- 23
- 33
5
votes
6 answers
Parsing empty options in Python
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…

directedition
- 11,145
- 18
- 58
- 79