I am putting the finishing touches on a cpan module. I'd like the module to have both a CLI and to be able to be called from within a script. I would like to integrate Getopt::Args with the script if at all possible but I'm not having any luck figuring out how I might accomplish this. From within a script, I am calling an exported function called anki_import and it is called with the name of a file and other args like so:

use Anki::Import; anki_import(<filename>, [--other, --args]);

From the command line, I'd like to be able to invoke the script with something like this: anki_import filename.txt --verbose

I'd like to have Getopt::Args or some similar module process arguments when calling the script from another script or from the command line. Hopefully this is clear. If not, this demonstrates my (failed) attempt at doing this:

package Anki::Import ; use strict; use warnings; use Path::Tiny; use Getopt::Args; use Log::Log4perl::Shortcuts qw(:all); use Exporter qw(import); our @EXPORT = qw(anki_import); # attempt to use Getop::Args opt verbose => ( isa => 'Bool', alias => 'v', comment => 'provide details on progress of Anki::Import' ); my $ref = optargs; <snip> sub anki_import { my $file = shift; # attempt to use Getopt::Args $ref variable to set up log levels if ($ref->{verbose}) { set_log_level('info'); } else { set_log_level('error'); } <snip> }

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks


In reply to SOLVED: Processing arguments from command line or a script by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.