If you don't mind introducing Moose and its dependencies, then MooseX::Getopt is a good way of setting up command-line options (some of the features that I'm using here are documented in MooseX::Getopt::Meta::Attribute)

For example, in MyApp.pm

package MyApp; use Moose; with 'MooseX::Getopt'; has 'extension' => ( traits => ['Getopt'], is => 'rw', isa => 'Str', cmd_aliases => 'b', documentation => 'Short description of extension' ); has 'liste_snp' => ( traits => ['Getopt'], is => 'rw', isa => 'Str', cmd_aliases => 'l' ); has 'min_snp_per_cds' => ( traits => ['Getopt'], is => 'rw', isa => 'Bool', cmd_aliases => 's', default => 1 ); has 'liste_geno' => ( traits => ['Getopt'], is => 'rw', isa => 'Str', cmd_aliases => 'g' ); has 'reference_cds_fasta' => ( traits => ['Getopt'], is => 'rw', isa => 'Str', cmd_aliases => 'r' ); has 'prefixe' => ( traits => ['Getopt'], is => 'rw', isa => 'Str', cmd_aliases => 'p' ); has 'min_read_classe' => ( traits => ['Getopt'], is => 'rw', isa => 'Int', cmd_aliases => 'mc', default => 30 ); has 'max_read_per_cent_ambi' => ( traits => ['Getopt'], is => 'rw', isa => 'Num', cmd_aliases => 'a', default => 1 ); sub print_options { my $self = shift; my $bam = $self->extension // 'undefined'; my $liste_snp = $self->liste_snp // 'undefined'; my $min_snp_cds = $self->min_snp_per_cds // 'undefined'; my $list_geno = $self->liste_geno // 'undefined'; my $ref_cds = $self->reference_cds_fasta // 'undefined'; my $prefixe = $self->prefixe // 'undefined'; my $seuil_min_classe = $self->min_read_classe // 'undefined'; my $seuil_max_amb = $self->max_read_per_cent_ambi // 'undefined'; print "extension: $bam\n"; print "liste_snp: $liste_snp\n"; print "min_snp_per_cds: $min_snp_cds\n"; print "list_geno: $list_geno\n"; print "reference_cds_fasta: $ref_cds\n"; print "prefixe: $prefixe\n"; print "min_read_classe: $seuil_min_classe\n"; print "max_read_per_cent_ambi: $seuil_max_amb\n"; } 1;
Then in runapp.pl,
#!/usr/bin/env perl use MyApp; my $app = MyApp->new_with_options(); $app->print_options; # ... rest of the script here exit;
Here are some examples of running the script...
The output of runapp.pl is:
extension: undefined liste_snp: undefined min_snp_per_cds: 1 list_geno: undefined reference_cds_fasta: undefined prefixe: undefined min_read_classe: 30 max_read_per_cent_ambi: 1
The output of runapp.pl --extention myext --mc 40 is:
extension: myext liste_snp: undefined min_snp_per_cds: 1 list_geno: undefined reference_cds_fasta: undefined prefixe: undefined min_read_classe: 40 max_read_per_cent_ambi: 1
The output of runapp.pl -? is:
usage: runapp.pl [-?abghlprs] [long options...] -h -? --usage --help Prints this usage informati +on. -b STR --extension STR Short description of extens +ion -l STR --liste_snp STR -s --min_snp_per_cds -g STR --liste_geno STR -r STR --reference_cds_fasta STR -p STR --prefixe STR --mc INT --min_read_classe INT -a NUM --max_read_per_cent_ambi NUM

In reply to Re: default option by kevbot
in thread default option by etricaen

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.