MooX::Options(3)
π MooX::Options(3) π
okay bestie, this is the User Contributed Perl Documentation page for MooX::Options β Explicit Options eXtension for Object Class. it's giving command-line main character energy, no cap β¨
β¨ NAME β¨
MooX::Options β Explicit Options eXtension for Object Class π
πΈ SYNOPSIS πΈ
okay so here's the whole vibe, bestie: π
In myOptions.pm :
package myOptions;
use Moo;
use MooX::Options;
option 'show_this_file' => (
is => 'ro',
format => 's',
required => 1,
doc => 'the file to display'
);
1;
In myTool.pl :
use myOptions;
use Path::Class;
my $opt = myOptions->new_with_options;
print "Content of the file : ",
file($opt->show_this_file)->slurp;
To use it :
perl myTool.pl --show_this_file=myFile.txt
Content of the file: myFile content
The help message :
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
The usage message :
perl myTool.pl --usage
USAGE: myTool.pl [ --show_this_file=String ] [ --usage ] [ --help ] [ --man ]
The manual :
perl myTool.pl --man
π¦ DESCRIPTION π¦
Create a command line tool with your Moo, Moose objects. literally iconic behavior β¨
Everything is explicit. You have an option keyword to replace the usual has to explicitly use your attribute into the command line. no sneaky surprises, we love that π
The option keyword takes additional parameters and uses Getopt::Long::Descriptive to generate a command line tool. slay π
π¨ IMPORTANT CHANGES IN 4.100 π¨
okay so 4.100 said βglow upβ and changed a bunch of stuff. let's get into it: π
π± Enhancing existing attributes π±
One can now convert an existing attribute into an option for obvious reasons. obsessed, actually π¦
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 '+attr' => (format => 's', 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 '/suits' => sub {
$my $reqh = Suitable::Web::Request::Handler->new( attr => config->{suit_attr} );
$reqh->all_suits;
};
dance;
1;
Of course there more ways to to it, Jedi or Catalyst shall be fine, either. periodt π«
π·οΈ Rename negativable into negatable π·οΈ
Since users stated that negativable 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. iykyk π«Ά
π Replace Locale::TextDomain by MooX::Locale::Passthrough π
Locale::TextDomain is broken (technically and functionally) and causes a lot of people to avoid MooX::Options or hack around. Both is unintened. not the vibe we want π
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. we love a flexible queen π
π§Έ Make lazy loaded features optional π§Έ
Since some features aren't used on a regular basis, their dependencies have been downgraded to "recommended" or "suggested". The optional features are:
β¨ autosplit β¨
This feature allowes one to split option arguments at a defined character and always return an array (implicit flag "repeatable"). cute and efficient π
option "search_path" => ( is => "ro", required => 1, autosplit => ":", format => "s" );
However, this feature requires following modules are provided:
Data::RecordRegexp::Common
π json format π
This feature allowes one to invoke a script like this little beauty:
$ my-tool --json-attr '{ "gem": "sapphire", "color": "blue" }'
It might be a reasonable enhancement to handles. ngl, fancy π
Handling JSON formatted arguments requires any of those modules are loded:
JSON::MaybeXSJSON::PP(in Core since 5.14).
π 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
$ my-tool --range 1..5
is all they desire, autosplit will enabled only when the dependencies of autosplit are fulfilled. boundaries, bestie π¦
π§° IMPORTED METHODS π§°
The list of the methods automatically imported into your class. freebies that actually slay β¨
πͺ new_with_options πͺ
It will parse your command line params and your inline params, validate and call the new method. multi-tasking icon π
myTool --str=ko
t->new_with_options()->str # ko
t->new_with_options(str => 'ok')->str #ok
ποΈ option ποΈ
The option keyword replaces the has method and adds support for special options for the command line only. glow-up unlocked π
See "OPTION PARAMETERS" for the documentation.
π£ options_usage | --help π£
It displays the usage message and returns the exit code. helpful queen behavior π
my $t = t->new_with_options();
my $exit_code = 1;
my $pre_message = "str is not valid";
$t->options_usage($exit_code, $pre_message);
This method is also automatically fired if the command option --help is passed.
myTool --help
π options_man | --man π
It displays the manual. the sacred texts β¨
my $t = t->new_with_options();
$t->options_man();
This is automatically fired if the command option --man is passed.
myTool --man
β‘ options_short_usage | --usage β‘
It displays a short version of the help message. snack-sized help π
my $t = t->new_with_options();
$t->options_short_usage($exit_code);
This is automatically fired if the command option --usage is passed.
myTool --usage
ποΈ IMPORT PARAMETERS ποΈ
The list of parameters supported by MooX::Options. okay bestie, let's customize the whole situation π
π¨ flavour π¨
Passes extra arguments for Getopt::Long::Descriptive. It is useful if you want to configure Getopt::Long. seasoning, but for CLI behavior π§
use MooX::Options flavour => [qw( pass_through )];
Any flavour is passed to Getopt::Long as a configuration, check the doc to see what is possible.
π‘οΈ protect_argv π‘οΈ
By default, @ARGV is protected. If you want to do something else on it, use this option and it will change the real @ARGV. handle with care, bestie π
use MooX::Options protect_argv => 0;
π« 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 option keyword will just work like an has. not me turning options off π
use MooX::Options skip_options => [qw/multi/];
π prefer_commandline π
By default, arguments passed to new_with_options have a higher priority than the command line options.
This parameter will give the command line an higher priority. command line gets the crown π
use MooX::Options prefer_commandline => 1;
π 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. layers, but make it organized π
myTool:
use MooX::Options with_config_from_file => 1;
In /etc/myTool.json:
{"test" : 1}
π with_locale_textdomain_oo π
This Parameter will load MooX::Locale::TextDomain::OO into your module as well as into MooX::Options::Descriptive::Usage. international icon β¨
No further action is taken, no language is chosen - everything keep in control.
Please read Locale::TextDomain::OO carefully how to enable the desired translation setup accordingly.
π usage_string π
This parameter is passed to Getopt::Long::Descriptive::describe_options() as the first parameter. first parameter, main character energy π
It is a "sprintf"-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 USAGE: %c %o.
%c will be replaced with what Getopt::Long::Descriptive thinks is the program name (it's computed from $0, see "prog_name").
%o will be replaced with a list of the short options, as well as the text [long options...] if any have been defined.
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.
Literal % characters will need to be written as %%, just like with sprintf. escape it, bestie π
π spacer π
This indicate the char to use for spacer. Please only use 1 char otherwize the text will be too long. one character only, we are not doing a paragraph spacer π
The default char is " ".
use MooX::Options space => '+'
Then the spacer_before and spacer_after will use it for man and help message. consistent spacing, consistent slay β¨
option 'x' => (is => 'ro', spacer_before => 1, spacer_after => 1);
π OPTION PARAMETERS π
The keyword option extends the keyword has with specific parameters for the command line. It's giving "same energy, but make it CLI," and I'm obsessed. π
π doc | documentation π
Documentation for the command line option. Literally the little blurb your users see, bestie.
π long_doc π
Documentation for the man page. By default, the doc parameter will be used.
See also Man parameters to get more examples of how to build a nice man page. π
π¨ required π¨
This attribute indicates that the parameter is mandatory. This attribute is not really used by MooX::Options but ensures that a consistent error message will be displayed.
π’ format π’
Format of the params, same as Getopt::Long::Descriptive.
iβ integeri@β array of integersβ strings@β array of stringfβ float value
By default, it's a boolean value.
Take a look at the available formats with Getopt::Long::Descriptive.
You need to understand that everything is explicit here. If you use Moose and your attribute has isa => 'Array[Int]', that will not imply the format i@. No sneaky inference, no cap. π«Ά
π format json : special format support π
The parameter will be treated like a json string.
option 'hash' => (is => 'ro', json => 1);
You can also use the json format:
option 'hash' => (is => 'ro', format => "json");
myTool --hash='{"a":1,"b":2}' # hash = { a => 1, b => 2 }
π ββοΈ negatable π ββοΈ
It adds the negative version for the option.
option 'verbose' => (is => 'ro', negatable => 1);
myTool --verbose # verbose = 1
myTool --no-verbose # verbose = 0
The former name of this flag, negativable, is discouraged β since it's not a word. We don't stan fake words here. π
π repeatable π
It appends to the format the array attribute @.
I advise you to add a default value to your attribute to always have an array. Otherwise the default value will be an undefined value. Bestie, protect your vibes with a default. β¨
option foo => (is => 'rw', format => 's@', default => sub { [] });
myTool --foo="abc" --foo="def" # foo = ["abc", "def"]
βοΈ autosplit βοΈ
For a repeatable option, you can add the autosplit feature with your specific parameters.
option test => (is => 'ro', format => 'i@', default => sub {[]}, autosplit => ',');
myTool --test=1 --test=2 # test = (1, 2)
myTool --test=1,2,3 # test = (1, 2, 3)
It will also handle quoted params with the autosplit.
option testStr => (is => 'ro', format => 's@', default => sub {[]}, autosplit => ',');
myTool --testStr='a,b,"c,d",e,f' # testStr ("a", "b", "c,d", "e", "f")
πΈ 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. Iconic behavior, honestly.
option test => (is => 'ro', format => 'i@', 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)
It will also handle quoted params like autosplit, and will not rangify them.
option testStr => (is => 'ro', format => 's@', default => sub {[]}, autorange => 1);
myTool --testStr='1,2,"3,a,4",5' # testStr (1, 2, "3,a,4", 5)
autosplit will be set to ',' if undefined. You may set autosplit to a different delimiter than ',' for your group separation, but the range operator '..' cannot be changed.
option testStr => (is => 'ro', format => 's@', default => sub {[]}, autorange => 1, autosplit => '-');
myTool --testStr='1-2-3-5..7' # testStr (1, 2, 3, 5, 6, 7)
π©³ short π©³
Long options can also have a short version or be aliased.
option 'verbose' => (is => 'ro', short => 'v');
myTool --verbose # verbose = 1
myTool -v # verbose = 1
option 'account_id' => (is => 'ro', format => 'i', short => 'a|id');
myTool --account_id=1
myTool -a=1
myTool --id=1
You can also use a shorter option without an attribute:
option 'account_id' => (is => 'ro', format => 'i');
myTool --acc=1
myTool --account=1
π’ 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 0, and options are sorted by their names.
option 'at_the_end' => (is => 'ro', order => 999);
π hidden π
Hide the option from the doc but still keep it as an option you can use on the command line.
option 'debug' => (is => 'ro', doc => 'hidden');
Or:
option 'debug' => (is => 'ro', hidden => 1);
π spacer_before, spacer_after π
Add a spacer before or after or both the params. Little breathing room, because your man page deserves main character energy.
option 'myoption' => (is => 'ro', spacer_before => 1, spacer_after => 1);
π COMPATIBILITY π
π MooX::Options and Mo π
MooX::Options is implemented as a frontend loader class and the real magic provided by a role composed into the caller by MooX::Options::import.
Since some required features (with, around) isn't provided by Mo, Class::Method::Modifiers must be loaded by any Mo class using MooX::Options, Role::Tiny::With is needed to inject the MooX::Options::Role and finally in the target package the private accessors to options_config and options_data are missing.
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. So likeβ¦ it's not banned, but it's not getting the full bestie treatment. π
If someome wants contribute guides how to use MooX::Options together with Mo or provide patches to solve this limitation - any support will granted. π«Ά
π ADDITIONAL MANUALS π
- Man parameters
- Using namespace::clean
- Manage your tools with MooX::Cmd
π EXTERNAL EXAMPLES π
- Slide3D about MooX::Options β
http://perltalks.celogeek.com/slides/2012/08/moox-options-slide3d.html
π¦ TRANSLATION π¦
Translation is now supported. Oui oui, ja ja, slay. π
Use the dzil command to update the pot and merge into the po files.
dzil msg-initβ Create a new language podzil msg-scanβ Scan and generate or update the pot filedzil msg-mergeβ Update all languages using the pot file
π THANKS π
- sschober β For implementation and German translation.
π THANKS π
- Matt S. Trout (mst)
<mst@shadowcat.co.uk>β For his patience and advice. - Tomas Doran (t0m)
<bobtfish@bobtfish.net>β To help me release the new version, and using it :) - Torsten Raudssus (Getty) β to use it a lot in DuckDuckGo
<http://duckduckgo.com>(go to see MooX module also) - Jens Rehsack (REHSACK) β Use with PkgSrc
<http://www.pkgsrc.org/>, and many really good idea (MooX::Cmd,MooX::Options, and more to come I'm sure) - All contributors β For improving and add more feature to
MooX::Options
π«Ά SUPPORT π«Ά
You can find documentation for this module with the perldoc command.
perldoc MooX::Options
You can also look for information at:
- RT: CPAN's request tracker (report bugs here) β
http://rt.cpan.org/NoAuth/Bugs.html?Dist=MooX-Options - AnnoCPAN: Annotated CPAN documentation β
http://annocpan.org/dist/MooX-Options - CPAN Ratings β
http://cpanratings.perl.org/d/MooX-Options - Search CPAN β
http://search.cpan.org/dist/MooX-Options/
π AUTHOR π
celogeek <me@celogeek.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2013 by celogeek <me@celogeek.com>.
This software is copyright (c) 2017 by Jens Rehsack.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
okay that's the whole page, bestie β this version was cooked up with perl v5.42.0, dated 2019-04-03, and it's the MooX::Options(3) man page. Periodt. π
β¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-MooX-Options 4.103-bp160.1.10 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms