.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "MooX::Options 3pm" .TH MooX::Options 3pm "2019-10-25" "perl v5.30.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" MooX::Options \- Explicit Options eXtension for Object Class .SH "SYNOPSIS" .IX Header "SYNOPSIS" In myOptions.pm : .PP .Vb 3 \& package myOptions; \& use Moo; \& use MooX::Options; \& \& option \*(Aqshow_this_file\*(Aq => ( \& is => \*(Aqro\*(Aq, \& format => \*(Aqs\*(Aq, \& required => 1, \& doc => \*(Aqthe file to display\*(Aq \& ); \& 1; .Ve .PP In myTool.pl : .PP .Vb 2 \& use myOptions; \& use Path::Class; \& \& my $opt = myOptions\->new_with_options; \& \& print "Content of the file : ", \& file($opt\->show_this_file)\->slurp; .Ve .PP To use it : .PP .Vb 2 \& perl myTool.pl \-\-show_this_file=myFile.txt \& Content of the file: myFile content .Ve .PP The help message : .PP .Vb 2 \& perl myTool.pl \-\-help \& USAGE: myTool.pl [\-h] [long options...] \& \& \-\-show_this_file: String \& the file to display \& \& \-h \-\-help: \& show this help message \& \& \-\-man: \& show the manual .Ve .PP The usage message : .PP .Vb 2 \& perl myTool.pl \-\-usage \& USAGE: myTool.pl [ \-\-show_this_file=String ] [ \-\-usage ] [ \-\-help ] [ \-\-man ] .Ve .PP The manual : .PP .Vb 1 \& perl myTool.pl \-\-man .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Create a command line tool with your Moo, Moose objects. .PP Everything is explicit. You have an \f(CW\*(C`option\*(C'\fR keyword to replace the usual \f(CW\*(C`has\*(C'\fR to explicitly use your attribute into the command line. .PP The \f(CW\*(C`option\*(C'\fR keyword takes additional parameters and uses Getopt::Long::Descriptive to generate a command line tool. .SH "IMPORTANT CHANGES IN 4.100" .IX Header "IMPORTANT CHANGES IN 4.100" .SS "Enhancing existing attributes" .IX Subsection "Enhancing existing attributes" One can now convert an existing attribute into an option for obvious reasons. .PP .Vb 1 \& package CommonRole; \& \& use Moo::Role; \& \& has attr => (is => "ro", ...); \& \& sub common_logic { ... } \& \& 1; \& \& package Suitable::Cmd::CLI; \& \& use Moo; \& use MooX::Cmd; \& use MooX::Options; \& \& with "CommonRole"; \& \& option \*(Aq+attr\*(Aq => (format => \*(Aqs\*(Aq, repeatable => 1); \& \& sub execute { shift\->common_logic } \& \& 1; \& \& package Suitable::Web::Request::Handler; \& \& use Moo; \& \& with "CommonRole"; \& \& sub all_suits { shift\->common_logic } \& \& 1; \& \& package Suitable::Web; \& \& use Dancer2; \& use Suitable::Web::Request::Handler; \& \& set serializer => "JSON"; \& \& get \*(Aq/suits\*(Aq => sub { \& $my $reqh = Suitable::Web::Request::Handler\->new( attr => config\->{suit_attr} ); \& $reqh\->all_suits; \& }; \& \& dance; \& \& 1; .Ve .PP Of course there more ways to to it, Jedi or Catalyst shall be fine, either. .SS "Rename negativable into negatable" .IX Subsection "Rename negativable into negatable" Since users stated that \f(CW\*(C`negativable\*(C'\fR is not a reasonable word, the flag is renamed into negatable. Those who will 2020 continue use negativable might or might not be warned about soon depreciation. .SS "Replace Locale::TextDomain by MooX::Locale::Passthrough" .IX Subsection "Replace Locale::TextDomain by MooX::Locale::Passthrough" Locale::TextDomain is broken (technically and functionally) and causes a lot of people to avoid \f(CW\*(C`MooX::Options\*(C'\fR or hack around. Both is unintened. .PP So introduce MooX::Locale::Passthrough to allow any vendor to add reasonable localization, eg. by composing MooX::Locale::TextDomain::OO into it's solution and initialize the localization in a reasonable way. .SS "Make lazy loaded features optional" .IX Subsection "Make lazy loaded features optional" Since some features aren't used on a regular basis, their dependencies have been downgraded to \f(CW\*(C`recommended\*(C'\fR or \f(CW\*(C`suggested\*(C'\fR. The optional features are: .IP "autosplit" 4 .IX Item "autosplit" This feature allowes one to split option arguments at a defined character and always return an array (implicit flag \f(CW\*(C`repeatable\*(C'\fR). .Sp .Vb 1 \& option "search_path" => ( is => "ro", required => 1, autosplit => ":", format => "s" ); .Ve .Sp However, this feature requires following modules are provided: .RS 4 .IP "\(bu" 4 Data::Record .IP "\(bu" 4 Regexp::Common .RE .RS 4 .RE .IP "json format" 4 .IX Item "json format" This feature allowes one to invoke a script like .Sp .Vb 1 \& $ my\-tool \-\-json\-attr \*(Aq{ "gem": "sapphire", "color": "blue" }\*(Aq .Ve .Sp It might be a reasonable enhancement to \fIhandles\fR. .Sp Handling \s-1JSON\s0 formatted arguments requires any of those modules are loded: .RS 4 .IP "\(bu" 4 JSON::MaybeXS .IP "\(bu" 4 \&\s-1JSON::PP\s0 (in Core since 5.14). .RE .RS 4 .RE .SS "Decouple autorange and autosplit" .IX Subsection "Decouple autorange and autosplit" Until 4.023, any option which had autorange enabled got autosplit enabled, too. Since autosplit might not work correctly and for a reasonable amount of users the fact of .PP .Vb 1 \& $ my\-tool \-\-range 1..5 .Ve .PP is all they desire, autosplit will enabled only when the dependencies of autosplit are fulfilled. .SH "IMPORTED METHODS" .IX Header "IMPORTED METHODS" The list of the methods automatically imported into your class. .SS "new_with_options" .IX Subsection "new_with_options" It will parse your command line params and your inline params, validate and call the \f(CW\*(C`new\*(C'\fR method. .PP .Vb 1 \& myTool \-\-str=ko \& \& t\->new_with_options()\->str # ko \& t\->new_with_options(str => \*(Aqok\*(Aq)\->str #ok .Ve .SS "option" .IX Subsection "option" The \f(CW\*(C`option\*(C'\fR keyword replaces the \f(CW\*(C`has\*(C'\fR method and adds support for special options for the command line only. .PP See \*(L"\s-1OPTION PARAMETERS\*(R"\s0 for the documentation. .SS "options_usage | \-\-help" .IX Subsection "options_usage | --help" It displays the usage message and returns the exit code. .PP .Vb 4 \& my $t = t\->new_with_options(); \& my $exit_code = 1; \& my $pre_message = "str is not valid"; \& $t\->options_usage($exit_code, $pre_message); .Ve .PP This method is also automatically fired if the command option \*(L"\-\-help\*(R" is passed. .PP .Vb 1 \& myTool \-\-help .Ve .SS "options_man | \-\-man" .IX Subsection "options_man | --man" It displays the manual. .PP .Vb 2 \& my $t = t\->new_with_options(); \& $t\->options_man(); .Ve .PP This is automatically fired if the command option \*(L"\-\-man\*(R" is passed. .PP .Vb 1 \& myTool \-\-man .Ve .SS "options_short_usage | \-\-usage" .IX Subsection "options_short_usage | --usage" It displays a short version of the help message. .PP .Vb 2 \& my $t = t\->new_with_options(); \& $t\->options_short_usage($exit_code); .Ve .PP This is automatically fired if the command option \*(L"\-\-usage\*(R" is passed. .PP .Vb 1 \& myTool \-\-usage .Ve .SH "IMPORT PARAMETERS" .IX Header "IMPORT PARAMETERS" The list of parameters supported by MooX::Options. .SS "flavour" .IX Subsection "flavour" Passes extra arguments for Getopt::Long::Descriptive. It is useful if you want to configure Getopt::Long. .PP .Vb 1 \& use MooX::Options flavour => [qw( pass_through )]; .Ve .PP Any flavour is passed to Getopt::Long as a configuration, check the doc to see what is possible. .SS "protect_argv" .IX Subsection "protect_argv" By default, \f(CW@ARGV\fR is protected. If you want to do something else on it, use this option and it will change the real \f(CW@ARGV\fR. .PP .Vb 1 \& use MooX::Options protect_argv => 0; .Ve .SS "skip_options" .IX Subsection "skip_options" If you have Role with options and you want to deactivate some of them, you can use this parameter. In that case, the \f(CW\*(C`option\*(C'\fR keyword will just work like an \f(CW\*(C`has\*(C'\fR. .PP .Vb 1 \& use MooX::Options skip_options => [qw/multi/]; .Ve .SS "prefer_commandline" .IX Subsection "prefer_commandline" By default, arguments passed to \f(CW\*(C`new_with_options\*(C'\fR have a higher priority than the command line options. .PP This parameter will give the command line an higher priority. .PP .Vb 1 \& use MooX::Options prefer_commandline => 1; .Ve .SS "with_config_from_file" .IX Subsection "with_config_from_file" This parameter will load MooX::Options in your module. The config option will be used between the command line and parameters. .PP myTool : .PP .Vb 1 \& use MooX::Options with_config_from_file => 1; .Ve .PP In /etc/myTool.json .PP .Vb 1 \& {"test" : 1} .Ve .SS "with_locale_textdomain_oo" .IX Subsection "with_locale_textdomain_oo" This Parameter will load MooX::Locale::TextDomain::OO into your module as well as into MooX::Options::Descriptive::Usage. .PP No further action is taken, no language is chosen \- everything keep in control. .PP Please read Locale::TextDomain::OO carefully how to enable the desired translation setup accordingly. .SH "usage_string" .IX Header "usage_string" This parameter is passed to \fBGetopt::Long::Descriptive::describe_options()\fR as the first parameter. .PP It is a \*(L"sprintf\*(R"\-like string that is used in generating the first line of the usage message. It's a one-line summary of how the command is to be invoked. The default value is \*(L"\s-1USAGE:\s0 \f(CW%c\fR \f(CW%o\fR\*(R". .PP \&\f(CW%c\fR will be replaced with what Getopt::Long::Descriptive thinks is the program name (it's computed from \f(CW$0\fR, see \*(L"prog_name\*(R"). .PP \&\f(CW%o\fR will be replaced with a list of the short options, as well as the text \&\*(L"[long options...]\*(R" if any have been defined. .PP The rest of the usage description can be used to summarize what arguments are expected to follow the program's options, and is entirely free-form. .PP Literal \*(L"%\*(R" characters will need to be written as \*(L"%%\*(R", just like with \&\*(L"sprintf\*(R". .SS "spacer" .IX Subsection "spacer" This indicate the char to use for spacer. Please only use 1 char otherwize the text will be too long. .PP The default char is \*(L" \*(R". .PP .Vb 1 \& use MooX::Options space => \*(Aq+\*(Aq .Ve .PP Then the \*(L"spacer_before\*(R" and \*(L"spacer_after\*(R" will use it for \*(L"man\*(R" and \*(L"help\*(R" message. .PP .Vb 1 \& option \*(Aqx\*(Aq => (is => \*(Aqro\*(Aq, spacer_before => 1, spacer_after => 1); .Ve .SH "OPTION PARAMETERS" .IX Header "OPTION PARAMETERS" The keyword \f(CW\*(C`option\*(C'\fR extend the keyword \f(CW\*(C`has\*(C'\fR with specific parameters for the command line. .SS "doc | documentation" .IX Subsection "doc | documentation" Documentation for the command line option. .SS "long_doc" .IX Subsection "long_doc" Documentation for the man page. By default the \f(CW\*(C`doc\*(C'\fR parameter will be used. .PP See also Man parameters to get more examples how to build a nice man page. .SS "required" .IX Subsection "required" This attribute indicates that the parameter is mandatory. This attribute is not really used by MooX::Options but ensures that consistent error message will be displayed. .SS "format" .IX Subsection "format" Format of the params, same as Getopt::Long::Descriptive. .IP "\(bu" 4 i : integer .IP "\(bu" 4 i@: array of integer .IP "\(bu" 4 s : string .IP "\(bu" 4 s@: array of string .IP "\(bu" 4 f : float value .PP By default, it's a boolean value. .PP Take a look of available formats with Getopt::Long::Descriptive. .PP You need to understand that everything is explicit here. If you use Moose and your attribute has \f(CW\*(C`isa => \*(AqArray[Int]\*(Aq\*(C'\fR, that will \fBnot\fR imply the format \f(CW\*(C`i@\*(C'\fR. .SS "format json : special format support" .IX Subsection "format json : special format support" The parameter will be treated like a json string. .PP .Vb 1 \& option \*(Aqhash\*(Aq => (is => \*(Aqro\*(Aq, json => 1); .Ve .PP You can also use the json format .PP .Vb 1 \& option \*(Aqhash\*(Aq => (is => \*(Aqro\*(Aq, format => "json"); \& \& myTool \-\-hash=\*(Aq{"a":1,"b":2}\*(Aq # hash = { a => 1, b => 2 } .Ve .SS "negatable" .IX Subsection "negatable" It adds the negative version for the option. .PP .Vb 1 \& option \*(Aqverbose\*(Aq => (is => \*(Aqro\*(Aq, negatable => 1); \& \& myTool \-\-verbose # verbose = 1 \& myTool \-\-no\-verbose # verbose = 0 .Ve .PP The former name of this flag, negativable, is discouraged \- since it's not a word. .SS "repeatable" .IX Subsection "repeatable" It appends to the \*(L"format\*(R" the array attribute \f(CW\*(C`@\*(C'\fR. .PP I advise to add a default value to your attribute to always have an array. Otherwise the default value will be an undefined value. .PP .Vb 1 \& option foo => (is => \*(Aqrw\*(Aq, format => \*(Aqs@\*(Aq, default => sub { [] }); \& \& myTool \-\-foo="abc" \-\-foo="def" # foo = ["abc", "def"] .Ve .SS "autosplit" .IX Subsection "autosplit" For repeatable option, you can add the autosplit feature with your specific parameters. .PP .Vb 1 \& option test => (is => \*(Aqro\*(Aq, format => \*(Aqi@\*(Aq, default => sub {[]}, autosplit => \*(Aq,\*(Aq); \& \& myTool \-\-test=1 \-\-test=2 # test = (1, 2) \& myTool \-\-test=1,2,3 # test = (1, 2, 3) .Ve .PP It will also handle quoted params with the autosplit. .PP .Vb 1 \& option testStr => (is => \*(Aqro\*(Aq, format => \*(Aqs@\*(Aq, default => sub {[]}, autosplit => \*(Aq,\*(Aq); \& \& myTool \-\-testStr=\*(Aqa,b,"c,d",e,f\*(Aq # testStr ("a", "b", "c,d", "e", "f") .Ve .SS "autorange" .IX Subsection "autorange" For another repeatable option you can add the autorange feature with your specific parameters. This allows you to pass number ranges instead of passing each individual number. .PP .Vb 1 \& option test => (is => \*(Aqro\*(Aq, format => \*(Aqi@\*(Aq, default => sub {[]}, autorange => 1); \& \& myTool \-\-test=1 \-\-test=2 # test = (1, 2) \& myTool \-\-test=1,2,3 # test = (1, 2, 3) \& myTool \-\-test=1,2,3..6 # test = (1, 2, 3, 4, 5, 6) .Ve .PP It will also handle quoted params like \f(CW\*(C`autosplit\*(C'\fR, and will not rangify them. .PP .Vb 1 \& option testStr => (is => \*(Aqro\*(Aq, format => \*(Aqs@\*(Aq, default => sub {[]}, autorange => 1); \& \& myTool \-\-testStr=\*(Aq1,2,"3,a,4",5\*(Aq # testStr (1, 2, "3,a,4", 5) .Ve .PP \&\f(CW\*(C`autosplit\*(C'\fR will be set to ',' if undefined. You may set \f(CW\*(C`autosplit\*(C'\fR to a different delimiter than ',' for your group separation, but the range operator '..' cannot be changed. .PP .Vb 1 \& option testStr => (is => \*(Aqro\*(Aq, format => \*(Aqs@\*(Aq, default => sub {[]}, autorange => 1, autosplit => \*(Aq\-\*(Aq); \& \& myTool \-\-testStr=\*(Aq1\-2\-3\-5..7\*(Aq # testStr (1, 2, 3, 5, 6, 7) .Ve .SS "short" .IX Subsection "short" Long option can also have short version or aliased. .PP .Vb 1 \& option \*(Aqverbose\*(Aq => (is => \*(Aqro\*(Aq, short => \*(Aqv\*(Aq); \& \& myTool \-\-verbose # verbose = 1 \& myTool \-v # verbose = 1 \& \& option \*(Aqaccount_id\*(Aq => (is => \*(Aqro\*(Aq, format => \*(Aqi\*(Aq, short => \*(Aqa|id\*(Aq); \& \& myTool \-\-account_id=1 \& myTool \-a=1 \& myTool \-\-id=1 .Ve .PP You can also use a shorter option without attribute : .PP .Vb 1 \& option \*(Aqaccount_id\*(Aq => (is => \*(Aqro\*(Aq, format => \*(Aqi\*(Aq); \& \& myTool \-\-acc=1 \& myTool \-\-account=1 .Ve .SS "order" .IX Subsection "order" Specifies the order of the attribute. If you want to push some attributes at the end of the list. By default all options have an order set to \f(CW0\fR, and options are sorted by their names. .PP .Vb 1 \& option \*(Aqat_the_end\*(Aq => (is => \*(Aqro\*(Aq, order => 999); .Ve .SS "hidden" .IX Subsection "hidden" Hide option from doc but still an option you can use on command line. .PP .Vb 1 \& option \*(Aqdebug\*(Aq => (is => \*(Aqro\*(Aq, doc => \*(Aqhidden\*(Aq); .Ve .PP Or .PP .Vb 1 \& option \*(Aqdebug\*(Aq => (is => \*(Aqro\*(Aq, hidden => 1); .Ve .SS "spacer_before, spacer_after" .IX Subsection "spacer_before, spacer_after" Add spacer before or after or both the params .PP .Vb 1 \& option \*(Aqmyoption\*(Aq => (is => \*(Aqro\*(Aq, spacer_before => 1, spacer_after => 1); .Ve .SH "COMPATIBILITY" .IX Header "COMPATIBILITY" .SS "MooX::Options and Mo" .IX Subsection "MooX::Options and Mo" \&\f(CW\*(C`MooX::Options\*(C'\fR is implemented as a frontend loader class and the real magic provided by a role composed into the caller by \f(CW\*(C`MooX::Options::import\*(C'\fR. .PP Since some required features (\f(CW\*(C`with\*(C'\fR, \f(CW\*(C`around\*(C'\fR) isn't provided by Mo, Class::Method::Modifiers must be loaded by any \f(CW\*(C`Mo\*(C'\fR class using \f(CW\*(C`MooX::Options\*(C'\fR, Role::Tiny::With is needed to \fIinject\fR the MooX::Options::Role and finally in the target package the private accessors to options_config and options_data are missing. .PP Concluding a reasonable support for Mo based classes is beyond the goal of this module. It's neither forbidden nor actively prevented, but won't be covered by any test nor actively supported. .PP If someome wants contribute guides how to use \f(CW\*(C`MooX::Options\*(C'\fR together with \&\f(CW\*(C`Mo\*(C'\fR or provide patches to solve this limitation \- any support will granted. .SH "ADDITIONAL MANUALS" .IX Header "ADDITIONAL MANUALS" .IP "\(bu" 4 Man parameters .IP "\(bu" 4 Using namespace::clean .IP "\(bu" 4 Manage your tools with MooX::Cmd .SH "EXTERNAL EXAMPLES" .IX Header "EXTERNAL EXAMPLES" .IP "\(bu" 4 Slide3D about MooX::Options .SH "Translation" .IX Header "Translation" Translation is now supported. .PP Use the dzil command to update the pot and merge into the po files. .IP "\(bu" 4 dzil msg-init .Sp Create a new language po .IP "\(bu" 4 dzil msg-scan .Sp Scan and generate or update the pot file .IP "\(bu" 4 dzil msg-merge .Sp Update all languages using the pot file .SS "\s-1THANKS\s0" .IX Subsection "THANKS" .IP "\(bu" 4 sschober .Sp For implementation and German translation. .SH "THANKS" .IX Header "THANKS" .IP "\(bu" 4 Matt S. Trout (mst) .Sp For his patience and advice. .IP "\(bu" 4 Tomas Doran (t0m) .Sp To help me release the new version, and using it :) .IP "\(bu" 4 Torsten Raudssus (Getty) .Sp to use it a lot in DuckDuckGo (go to see MooX module also) .IP "\(bu" 4 Jens Rehsack (\s-1REHSACK\s0) .Sp Use with PkgSrc , and many really good idea (MooX::Cmd, MooX::Options, and more to come I'm sure) .IP "\(bu" 4 All contributors .Sp For improving and add more feature to MooX::Options .SH "SUPPORT" .IX Header "SUPPORT" You can find documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc MooX::Options .Ve .PP You can also look for information at: .IP "\(bu" 4 \&\s-1RT: CPAN\s0's request tracker (report bugs here) .Sp .IP "\(bu" 4 AnnoCPAN: Annotated \s-1CPAN\s0 documentation .Sp .IP "\(bu" 4 \&\s-1CPAN\s0 Ratings .Sp .IP "\(bu" 4 Search \s-1CPAN\s0 .Sp .SH "AUTHOR" .IX Header "AUTHOR" celogeek .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2013 by celogeek . .PP This software is copyright (c) 2017 by Jens Rehsack. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.