MooseX:...e::Trait(3)

πŸ“¦
version 0.10 Β· 2 versions
, in fedora 43

πŸ’– NAME πŸ’–

MooseX::Role::Cmd::Meta::Attribute::Trait β€” the optional meta attribute trait that lets you give your command-line options custom names πŸŽ€

(oh and psst, bestie, this one lives in the User Contributed Perl Documentation section of your manual, so you know it's the good stuff ✨)


🌸 SYNOPSIS 🌸

okay so picture this: you're building a command-line thing and you want your attributes to just know how they should show up as options. boom. here's the whole vibe:

package MyApp::Cmd::SomeScript;

with 'MooseX::Role::Cmd';

has 'basic'   => (
    isa             => 'Str',
    is              => 'rw',
);

has 'prefix'   => (
    traits          => [ 'CmdOpt' ],
    isa             => 'Str',
    is              => 'rw',
    cmdopt_prefix   => '-',
);

has 'rename'   => (
    traits          => [ 'CmdOpt' ],
    isa             => 'Str',
    is              => 'rw',
    cmdopt_name     => '+alt_name',
);

$cmd = MyApp::Cmd::SomeScript->new( basic => 'foo', prefix => 'bar', rename => 'foobar' );

$cmd->run();

# somescript --basic foo -prefix bar +alt_name foobar

like, look at that last line 😭 it literally just works. -prefix instead of --prefix? +alt_name instead of rename? yes queen. we love a flexible trait. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

So what this module actually does: it hands MooseX::Role::Cmd a lil bit of extra markup so that role can figure out how to use your command line parameters. that's the whole job. it's giving "backstage crew for your CLI" energy and honestly? iconic. πŸ‘‘


✨ ATTRIBUTES ✨

πŸŽ€ cmdopt_prefix

Forces the command prefix to be a certain character. This was introduced to allow parameters to be specified as -param or --param

so like, if you're tired of everything defaulting to -- and you want a single lil dash era, this is your girl. 🫢

πŸ’œ has_cmdopt_prefix

Test for attribute above

🌈 cmdopt_name

Forces the command options name to be the passed string. This was introduced to allow parameters to have a different name to the attribute.

heads up bestie: This option will override the cmdopt_prefix attribute. so if you set both, cmdopt_name wins. no cap. πŸ’€

πŸ“ has_cmdopt_name

Test for attribute above

🧚 cmdopt_env

This attribute trait can be used to specify an environment variable rather than a command line option.

has 'home_dir' => (
    traits => [ 'CmdOpt' ],
    is => 'rw',
    isa => 'Str',
    cmdopt_env => 'APP_HOME',
    default => '/my/app/home'
);

# $ENV{APP_HOME} = /my/app/home
not everything has to be a flag!! sometimes the vibe lives in the environment instead and this trait is how you say so. 🌱

⭐ has_cmdopt_env

Test for attribute above


πŸ’€ BUGS πŸ’€

All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.

fr fr, every single piece of complex software has bugs hiding in it somewhere and this module is not the exception πŸ™ˆ if you catch one, either email the author or drop it in cpan-RT. don't suffer in silence, bestie.

πŸ‘‘ AUTHOR πŸ‘‘

Ian Sillitoe <ian.sillitoe@gmail.com>


πŸ”₯ SEE ALSO πŸ”₯

The idea for this code was ripped kicking and screaming from MooseX::Getopt::Meta::Attribute::Trait

literally kicking and screaming, that's the lore, I don't make the rules 😭

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


okay that's the whole page, you made it, I'm so proud of you πŸ₯Ήβœ¨

this lil beauty comes to you as perl v5.42.0, dated 2025-07-25, filed under MooseX:...e::Trait(3) β€” go forth and name your options whatever your heart desires πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from