in reply to Re^2: App::Rad is pretty rad for creating command line apps, but... (need help!)
in thread App::Rad is pretty rad for creating command line apps, but... (need help!)
Wow, it's been a while - both here and in App::Rad!
First of all, thanks for using App::Rad. I've been meaning to rewrite it from scratch in a much saner/cleaner/expandable way but never got around to it. Still, I'm glad you found it useful!
As for your question, if you're doing the same thing in all your commands, you should know that, as documented, App::Rad will optionally read a "setup" sub every time your app starts. People have used this to setup DBI handles, reading configuration files and whatnot.
There is also the "pre_process" function, which is like a "before" hook in Moose (but for all commands), and you can use it to parse $c->argv *after* Rad knows the command it's going to run, and put some things on the stash (like $data and $name).
With the code you shared here, I think you could try something like:
fun pre_process ($c) { my ($data, $data_type) = @{_process_data($c)}; $c->stash->{data} = $data; $c->stash->{data_type} = $data_type; $c->stash->{name} = $c->argv->[0]; } fun add ($c) { $c->stash->{data}->add( $c->stash->{name} ); }
Not as pretty as you might have hoped, but still pretty straightforward, I think :)
Note that, if you need to behave differently on some commands, you can check $c->cmd to see the name of the current command from within pre_process().
I'll keep method modifiers (as well as subcommands, which is what you seem to be doing) in mind if I ever get to that rewrite. Meanwhile, I hope this helps!
Cheers!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: App::Rad is pretty rad for creating command line apps, but... (need help!)
by mascip (Pilgrim) on Mar 30, 2014 at 12:53 UTC | |
|
Re^4: App::Rad is pretty rad for creating command line apps, but... (need help!)
by 7stud (Deacon) on Mar 29, 2014 at 23:32 UTC |