Coding Guidelines/Command-line Processing

From The Open Source Backup Wiki (Amanda, MySQL Backup, BackupPC)

Jump to: navigation, search

All Amanda applications should process their commandlines with Perl's Getopt::Long module, for consistency. Moreover, they should use the bundling configuration flag -- this allows short command line options to be concatenated with their arguments as in -ofoo=bar.

All applications should accept both short and long options for all common operations. Where possible, applications should accept -o to overwrite configuration options.

Here's a typical command-line handling sequence:

use Getopt::Long;

# ...

sub usage {
    print <<EOF;
Usage: amprototype <config> [-o configoption]* [-x] [-y foo]
  ...
EOF
    exit(1);
}

# ...

my $config_overwrites = new_config_overwrites($#ARGV+1);
Getopt::Long::Configure(qw{bundling});
GetOptions(
    'x' => \$opt_x,
    'y=s' => \$opt_y,
    'o=s' => sub { add_config_overwrite_opt($config_overwrites, $_[1]); },
) or usage();
usage() unless (@ARGV == 1);
my $config_name = $ARGV[0];

# ...

my $cfg_ok = config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
apply_config_overwrites($config_overwrites);

See the source to one of the simpler Amanda applications for the definitive pattern to follow, and if this page has become outdated, please update it!

Personal tools