@@ -55,7 +55,14 @@ The class also has method #help, which displays automatically-generated help tex
55
55
- {Argument Converters}[#label-Argument+Converters]
56
56
- {Help}[#label-Help]
57
57
- {Top List and Base List}[#label-Top+List+and+Base+List]
58
- - {Methods for Defining Options}[#label-Methods+for+Defining+Options]
58
+ - {Defining Options}[#label-Defining+Options]
59
+ - {Parsing}[#label-Parsing]
60
+ - {Method parse!}[#label-Method+parse-21]
61
+ - {Method parse}[#label-Method+parse]
62
+ - {Method order!}[#label-Method+order-21]
63
+ - {Method order}[#label-Method+order]
64
+ - {Method permute!}[#label-Method+permute-21]
65
+ - {Method permute}[#label-Method+permute]
59
66
60
67
=== To Begin With
61
68
@@ -619,7 +626,7 @@ The stack includes:
619
626
When \OptionParser builds its help text, the options in the top list
620
627
precede those in the base list.
621
628
622
- === Methods for Defining Options
629
+ === Defining Options
623
630
624
631
Option-defining methods allow you to create an option, and also append/prepend it
625
632
to the top list or append it to the base list.
@@ -658,3 +665,171 @@ here's the core method for defining an option:
658
665
option names, and other values;
659
666
others return either the created option object
660
667
or the parser object +self+.
668
+
669
+ === Parsing
670
+
671
+ \OptionParser has six instance methods for parsing.
672
+
673
+ Three have names ending with a "bang" (<tt>!</tt>):
674
+
675
+ - parse!
676
+ - order!
677
+ - permute!
678
+
679
+ Each of these methods:
680
+
681
+ - Accepts an optional array of string arguments +argv+;
682
+ if not given, +argv+ defaults to the value of OptionParser#default_argv,
683
+ whose initial value is ARGV.
684
+ - Accepts an optional keyword argument +into+
685
+ (see {Keyword Argument into}[#label-Keyword+Argument+into]).
686
+ - Returns +argv+, possibly with some elements removed.
687
+
688
+ The three other methods have names _not_ ending with a "bang":
689
+
690
+ - parse
691
+ - order
692
+ - permute
693
+
694
+ Each of these methods:
695
+
696
+ - Accepts an array of string arguments
697
+ _or_ zero or more string arguments.
698
+ - Accepts an optional keyword argument +into+ and its value _into_.
699
+ (see {Keyword Argument into}[#label-Keyword+Argument+into]).
700
+ - Returns +argv+, possibly with some elements removed.
701
+
702
+ ==== \Method parse!
703
+
704
+ \Method parse!:
705
+
706
+ - Accepts an optional array of string arguments +argv+;
707
+ if not given, +argv+ defaults to the value of OptionParser#default_argv,
708
+ whose initial value is ARGV.
709
+ - Accepts an optional keyword argument +into+
710
+ (see {Keyword Argument into}[#label-Keyword+Argument+into]).
711
+ - Returns +argv+, possibly with some elements removed.
712
+
713
+ The method processes the elements in +argv+ beginning at <tt>argv[0]</tt>,
714
+ and ending, by default, at the end.
715
+
716
+ Otherwise processing ends and the method returns when:
717
+
718
+ - The terminator argument <tt>--</tt> is found;
719
+ the terminator argument is removed before the return.
720
+ - Environment variable +POSIXLY_CORRECT+ is defined
721
+ and a non-option argument is found;
722
+ the non-option argument is not removed.
723
+ Note that the _value_ of that variable does not matter,
724
+ as only its existence is checked.
725
+
726
+ File +parse_bang.rb+:
727
+
728
+ :include: ruby/parse_bang.rb
729
+
730
+ Help:
731
+
732
+ $ ruby parse_bang.rb --help
733
+ Usage: parse_bang [options]
734
+ --xxx
735
+ --yyy YYY
736
+ --zzz [ZZZ]
737
+
738
+ Default behavior:
739
+
740
+ $ ruby parse_bang.rb input_file.txt output_file.txt --xxx --yyy FOO --zzz BAR
741
+ ["--xxx", true]
742
+ ["--yyy", "FOO"]
743
+ ["--zzz", "BAR"]
744
+ Returned: ["input_file.txt", "output_file.txt"] (Array)
745
+
746
+ Processing ended by terminator argument:
747
+
748
+ $ ruby parse_bang.rb input_file.txt output_file.txt --xxx --yyy FOO -- --zzz BAR
749
+ ["--xxx", true]
750
+ ["--yyy", "FOO"]
751
+ Returned: ["input_file.txt", "output_file.txt", "--zzz", "BAR"] (Array)
752
+
753
+ Processing ended by non-option found when +POSIXLY_CORRECT+ is defined:
754
+
755
+ $ POSIXLY_CORRECT=true ruby parse_bang.rb --xxx input_file.txt output_file.txt -yyy FOO
756
+ ["--xxx", true]
757
+ Returned: ["input_file.txt", "output_file.txt", "-yyy", "FOO"] (Array)
758
+
759
+ ==== \Method parse
760
+
761
+ \Method parse:
762
+
763
+ - Accepts an array of string arguments
764
+ _or_ zero or more string arguments.
765
+ - Accepts an optional keyword argument +into+ and its value _into_.
766
+ (see {Keyword Argument into}[#label-Keyword+Argument+into]).
767
+ - Returns +argv+, possibly with some elements removed.
768
+
769
+ If given an array +ary+, the method forms array +argv+ as <tt>ary.dup</tt>.
770
+ If given zero or more string arguments, those arguments are formed
771
+ into array +argv+.
772
+
773
+ The method calls
774
+
775
+ parse!(argv, into: into)
776
+
777
+ Note that environment variable +POSIXLY_CORRECT+
778
+ and the terminator argument <tt>--</tt> are honored.
779
+
780
+ File +parse.rb+:
781
+
782
+ :include: ruby/parse.rb
783
+
784
+ Help:
785
+
786
+ $ ruby parse.rb --help
787
+ Usage: parse [options]
788
+ --xxx
789
+ --yyy YYY
790
+ --zzz [ZZZ]
791
+
792
+ Default behavior:
793
+
794
+ $ ruby parse.rb input_file.txt output_file.txt --xxx --yyy FOO --zzz BAR
795
+ ["--xxx", true]
796
+ ["--yyy", "FOO"]
797
+ ["--zzz", "BAR"]
798
+ Returned: ["input_file.txt", "output_file.txt"] (Array)
799
+
800
+ Processing ended by terminator argument:
801
+
802
+ $ ruby parse.rb input_file.txt output_file.txt --xxx --yyy FOO -- --zzz BAR
803
+ ["--xxx", true]
804
+ ["--yyy", "FOO"]
805
+ Returned: ["input_file.txt", "output_file.txt", "--zzz", "BAR"] (Array)
806
+
807
+ Processing ended by non-option found when +POSIXLY_CORRECT+ is defined:
808
+
809
+ $ POSIXLY_CORRECT=true ruby parse.rb --xxx input_file.txt output_file.txt -yyy FOO
810
+ ["--xxx", true]
811
+ Returned: ["input_file.txt", "output_file.txt", "-yyy", "FOO"] (Array)
812
+
813
+ ==== \Method order!
814
+
815
+ Calling method OptionParser#order! gives exactly the same result as
816
+ calling method OptionParser#parse! with environment variable
817
+ +POSIXLY_CORRECT+ defined.
818
+
819
+ ==== \Method order
820
+
821
+ Calling method OptionParser#order gives exactly the same result as
822
+ calling method OptionParser#parse with environment variable
823
+ +POSIXLY_CORRECT+ defined.
824
+
825
+ ==== \Method permute!
826
+
827
+ Calling method OptionParser#permute! gives exactly the same result as
828
+ calling method OptionParser#parse! with environment variable
829
+ +POSIXLY_CORRECT+ _not_ defined.
830
+
831
+ ==== \Method permute
832
+
833
+ Calling method OptionParser#permute gives exactly the same result as
834
+ calling method OptionParser#parse with environment variable
835
+ +POSIXLY_CORRECT+ _not_ defined.
0 commit comments