You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+31-1Lines changed: 31 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,4 +90,34 @@ If you want to contribute, you can fork this repository, make your changes and s
90
90
However, improvements must be one of the following:
91
91
92
92
* use fewer lines of code, without sacrificing readablity or functionality
93
-
* enhance readablity or functionality, without increasing the lines of code
93
+
* enhance readablity or functionality, without increasing the lines of code
94
+
95
+
Frequently Asked Questions
96
+
--------------------------
97
+
98
+
### All my argument values are either true or false - what's wrong?
99
+
You must define default values, if the option should accept an argument. Every option without a default value (or with `true` or `false` as default) is treated as a switch: true if given and false / default otherwise.
100
+
101
+
### Is it possible to define mandatory / required arguments, which must be provided?
102
+
No it's not. It should be possible in any case to provide a reasonable default value. If you come across a case where it's not possible, feel free to contact me.
103
+
104
+
### Are long arguments with spaces and other special characters allowed?
105
+
Yes, just define an option which takes a `String` as an argument, i.e. pass a string as the default value for that option. Now everything between quotes will be parsed as the value for that argument, e.g. `ruby testscript.rb --selection 'I want the best selection you have!!! And if possible, add Donuts.'` Note that double quotes may cause trouble, for example I get an error if I use an exclamation mark in double quotes, but no error in single quotes.
106
+
107
+
### Is it possible to define arguments which accept lists / arrays / multiple files / ... ?
108
+
Yes, just define an option which takes an `Array` as an argument, i.e. pass an array as the default value for that option. The input will be split by comma. If the arguments contain spaces, wrap the whole thing in single quotes or double quotes.
109
+
110
+
For example if you want to accept multiple file names with whitespaces in them:
111
+
112
+
require 'rubygems' # necessary for ruby v1.8.*
113
+
require 'micro-optparse'
114
+
115
+
options = Parser.new do |p|
116
+
p.option :filenames, "Files which will be processed", :default => []
117
+
end.process!
118
+
119
+
p options[:filenames]
120
+
121
+
122
+
ruby testscript.rb --filenames 'todo.txt,my great adventures.txt'
0 commit comments