---
name: "MooX::Cmd::Role"
section: "3pm"
platform: "linux"
package: "libmoox-cmd-perl"
version: "0.017-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/MooX::Cmd::Role"
---

# MooX::Cmd::Role(3pm)

MooX cli app commands do this

## NAME

MooX::Cmd::Role - MooX cli app commands do this

## SYNOPSIS

### using role and want behavior as MooX::Cmd

```
  package MyFoo;

  with MooX::Cmd::Role;

  sub _build_command_execute_from_new { 1 }
  package main;
  my $cmd = MyFoo->new_with_cmd;
```

### using role and don't execute immediately

```
  package MyFoo;
  with MooX::Cmd::Role;
  use List::MoreUtils qw/ first_idx /;
  sub _build_command_base { "MyFoo::Command" }
  sub _build_command_execute_from_new { 0 }
  sub execute {
      my $self = shift;
      my $chain_idx = first_idx { $self == $_ } @{$self->command_chain};
      my $next_cmd = $self->command_chain->{$chain_idx+1};
      $next_cmd->owner($self);
      $next_cmd->execute;
  }
  package main;
  my $cmd = MyFoo->new_with_cmd;
  $cmd->command_chain->[-1]->run();
```

### explicit expression of some implicit stuff

```
  package MyFoo;
  with MooX::Cmd::Role;
  sub _build_command_base { "MyFoo::Command" }
  sub _build_command_execute_method_name { "run" }
  sub _build_command_execute_from_new { 0 }
  package main;
  my $cmd = MyFoo->new_with_cmd;
  $cmd->command_chain->[-1]->run();
```

## DESCRIPTION

MooX::Cmd::Role is made for modern, flexible Moo style to tailor cli commands.

## ATTRIBUTES

### command\_args

ARRAY-REF of args on command line

### command\_chain

ARRAY-REF of commands lead to this instance

### command\_chain\_end

COMMAND accesses the finally detected command in chain

### command\_name

ARRAY-REF the name of the command lead to this command

### command\_commands

HASH-REF names of other commands

### command\_base

STRING base of command plugins

### command\_execute\_method\_name

STRING name of the method to invoke to execute a command, default "execute"

### command\_execute\_return\_method\_name

STRING I have no clue what that is good for ...

### command\_creation\_method\_name

STRING name of constructor

### command\_creation\_chain\_methods

ARRAY-REF names of methods to chain for creating object (from "command\_creation\_method\_name")

### command\_execute\_from\_new

BOOL true when constructor shall invoke "command\_execute\_method\_name", false otherwise

## METHODS

### new\_with\_cmd

initializes by searching command line args for commands and invoke them

### execute\_return

returns the content of `$self`-\>{execute\_return}

## LICENSE AND COPYRIGHT

Copyright 2012-2013 Torsten Raudssus, Copyright 2013-2017 Jens Rehsack.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See \<http://dev.perl.org/licenses/\> for more information.
