-
Notifications
You must be signed in to change notification settings - Fork 26
More on tutorial #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
More on tutorial #24
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| require 'optparse' | ||
| parser = OptionParser.new | ||
| parser.on('--xxx') do |value| | ||
| p ['--xxx', value] | ||
| end | ||
| parser.on('--yyy YYY') do |value| | ||
| p ['--yyy', value] | ||
| end | ||
| parser.on('--zzz [ZZZ]') do |value| | ||
| p ['--zzz', value] | ||
| end | ||
| ret = parser.parse(ARGV) | ||
| puts "Returned: #{ret} (#{ret.class})" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| require 'optparse' | ||
| parser = OptionParser.new | ||
| parser.on('--xxx') do |value| | ||
| p ['--xxx', value] | ||
| end | ||
| parser.on('--yyy YYY') do |value| | ||
| p ['--yyy', value] | ||
| end | ||
| parser.on('--zzz [ZZZ]') do |value| | ||
| p ['--zzz', value] | ||
| end | ||
| ret = parser.parse! | ||
| puts "Returned: #{ret} (#{ret.class})" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,14 @@ The class also has method #help, which displays automatically-generated help tex | |
| - {Argument Converters}[#label-Argument+Converters] | ||
| - {Help}[#label-Help] | ||
| - {Top List and Base List}[#label-Top+List+and+Base+List] | ||
| - {Methods for Defining Options}[#label-Methods+for+Defining+Options] | ||
| - {Defining Options}[#label-Defining+Options] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This label conflicts with another line. |
||
| - {Parsing}[#label-Parsing] | ||
| - {Method parse!}[#label-Method+parse-21] | ||
| - {Method parse}[#label-Method+parse] | ||
| - {Method order!}[#label-Method+order-21] | ||
| - {Method order}[#label-Method+order] | ||
| - {Method permute!}[#label-Method+permute-21] | ||
| - {Method permute}[#label-Method+permute] | ||
|
|
||
| === To Begin With | ||
|
|
||
|
|
@@ -619,7 +626,7 @@ The stack includes: | |
| When \OptionParser builds its help text, the options in the top list | ||
| precede those in the base list. | ||
|
|
||
| === Methods for Defining Options | ||
| === Defining Options | ||
|
|
||
| Option-defining methods allow you to create an option, and also append/prepend it | ||
| to the top list or append it to the base list. | ||
|
|
@@ -658,3 +665,171 @@ here's the core method for defining an option: | |
| option names, and other values; | ||
| others return either the created option object | ||
| or the parser object +self+. | ||
|
|
||
| === Parsing | ||
|
|
||
| \OptionParser has six instance methods for parsing. | ||
|
|
||
| Three have names ending with a "bang" (<tt>!</tt>): | ||
|
|
||
| - parse! | ||
| - order! | ||
| - permute! | ||
|
|
||
| Each of these methods: | ||
|
|
||
| - Accepts an optional array of string arguments +argv+; | ||
| if not given, +argv+ defaults to the value of OptionParser#default_argv, | ||
| whose initial value is ARGV. | ||
| - Accepts an optional keyword argument +into+ | ||
| (see {Keyword Argument into}[#label-Keyword+Argument+into]). | ||
| - Returns +argv+, possibly with some elements removed. | ||
|
|
||
| The three other methods have names _not_ ending with a "bang": | ||
|
|
||
| - parse | ||
| - order | ||
| - permute | ||
|
|
||
| Each of these methods: | ||
|
|
||
| - Accepts an array of string arguments | ||
| _or_ zero or more string arguments. | ||
| - Accepts an optional keyword argument +into+ and its value _into_. | ||
| (see {Keyword Argument into}[#label-Keyword+Argument+into]). | ||
| - Returns +argv+, possibly with some elements removed. | ||
|
|
||
| ==== \Method parse! | ||
|
|
||
| \Method parse!: | ||
|
|
||
| - Accepts an optional array of string arguments +argv+; | ||
| if not given, +argv+ defaults to the value of OptionParser#default_argv, | ||
| whose initial value is ARGV. | ||
| - Accepts an optional keyword argument +into+ | ||
| (see {Keyword Argument into}[#label-Keyword+Argument+into]). | ||
| - Returns +argv+, possibly with some elements removed. | ||
|
|
||
| The method processes the elements in +argv+ beginning at <tt>argv[0]</tt>, | ||
| and ending, by default, at the end. | ||
|
|
||
| Otherwise processing ends and the method returns when: | ||
|
|
||
| - The terminator argument <tt>--</tt> is found; | ||
| the terminator argument is removed before the return. | ||
| - Environment variable +POSIXLY_CORRECT+ is defined | ||
| and a non-option argument is found; | ||
| the non-option argument is not removed. | ||
| Note that the _value_ of that variable does not matter, | ||
| as only its existence is checked. | ||
|
|
||
| File +parse_bang.rb+: | ||
|
|
||
| :include: ruby/parse_bang.rb | ||
|
|
||
| Help: | ||
|
|
||
| $ ruby parse_bang.rb --help | ||
| Usage: parse_bang [options] | ||
| --xxx | ||
| --yyy YYY | ||
| --zzz [ZZZ] | ||
|
|
||
| Default behavior: | ||
|
|
||
| $ ruby parse_bang.rb input_file.txt output_file.txt --xxx --yyy FOO --zzz BAR | ||
| ["--xxx", true] | ||
| ["--yyy", "FOO"] | ||
| ["--zzz", "BAR"] | ||
| Returned: ["input_file.txt", "output_file.txt"] (Array) | ||
|
|
||
| Processing ended by terminator argument: | ||
|
|
||
| $ ruby parse_bang.rb input_file.txt output_file.txt --xxx --yyy FOO -- --zzz BAR | ||
| ["--xxx", true] | ||
| ["--yyy", "FOO"] | ||
| Returned: ["input_file.txt", "output_file.txt", "--zzz", "BAR"] (Array) | ||
|
|
||
| Processing ended by non-option found when +POSIXLY_CORRECT+ is defined: | ||
|
|
||
| $ POSIXLY_CORRECT=true ruby parse_bang.rb --xxx input_file.txt output_file.txt -yyy FOO | ||
| ["--xxx", true] | ||
| Returned: ["input_file.txt", "output_file.txt", "-yyy", "FOO"] (Array) | ||
|
|
||
| ==== \Method parse | ||
|
|
||
| \Method parse: | ||
|
|
||
| - Accepts an array of string arguments | ||
| _or_ zero or more string arguments. | ||
| - Accepts an optional keyword argument +into+ and its value _into_. | ||
| (see {Keyword Argument into}[#label-Keyword+Argument+into]). | ||
| - Returns +argv+, possibly with some elements removed. | ||
|
|
||
| If given an array +ary+, the method forms array +argv+ as <tt>ary.dup</tt>. | ||
| If given zero or more string arguments, those arguments are formed | ||
| into array +argv+. | ||
|
|
||
| The method calls | ||
|
|
||
| parse!(argv, into: into) | ||
|
|
||
| Note that environment variable +POSIXLY_CORRECT+ | ||
| and the terminator argument <tt>--</tt> are honored. | ||
|
|
||
| File +parse.rb+: | ||
|
|
||
| :include: ruby/parse.rb | ||
|
|
||
| Help: | ||
|
|
||
| $ ruby parse.rb --help | ||
| Usage: parse [options] | ||
| --xxx | ||
| --yyy YYY | ||
| --zzz [ZZZ] | ||
|
|
||
| Default behavior: | ||
|
|
||
| $ ruby parse.rb input_file.txt output_file.txt --xxx --yyy FOO --zzz BAR | ||
| ["--xxx", true] | ||
| ["--yyy", "FOO"] | ||
| ["--zzz", "BAR"] | ||
| Returned: ["input_file.txt", "output_file.txt"] (Array) | ||
|
|
||
| Processing ended by terminator argument: | ||
|
|
||
| $ ruby parse.rb input_file.txt output_file.txt --xxx --yyy FOO -- --zzz BAR | ||
| ["--xxx", true] | ||
| ["--yyy", "FOO"] | ||
| Returned: ["input_file.txt", "output_file.txt", "--zzz", "BAR"] (Array) | ||
|
|
||
| Processing ended by non-option found when +POSIXLY_CORRECT+ is defined: | ||
|
|
||
| $ POSIXLY_CORRECT=true ruby parse.rb --xxx input_file.txt output_file.txt -yyy FOO | ||
| ["--xxx", true] | ||
| Returned: ["input_file.txt", "output_file.txt", "-yyy", "FOO"] (Array) | ||
|
|
||
| ==== \Method order! | ||
|
|
||
| Calling method OptionParser#order! gives exactly the same result as | ||
| calling method OptionParser#parse! with environment variable | ||
| +POSIXLY_CORRECT+ defined. | ||
|
|
||
| ==== \Method order | ||
|
|
||
| Calling method OptionParser#order gives exactly the same result as | ||
| calling method OptionParser#parse with environment variable | ||
| +POSIXLY_CORRECT+ defined. | ||
|
|
||
| ==== \Method permute! | ||
|
|
||
| Calling method OptionParser#permute! gives exactly the same result as | ||
| calling method OptionParser#parse! with environment variable | ||
| +POSIXLY_CORRECT+ _not_ defined. | ||
|
|
||
| ==== \Method permute | ||
|
|
||
| Calling method OptionParser#permute gives exactly the same result as | ||
| calling method OptionParser#parse with environment variable | ||
| +POSIXLY_CORRECT+ _not_ defined. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this section also dropped an inconsistent naming (--nosuch/--other_option).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean. Is this drop ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is excellent is what I mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
K, thanks!