Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a help command to a script

Is there a standard way of adding a help function to a script? The simplest way would maybe to take an argument and print some text if it's "-help" or something. Does anyone have any examples on how to do this?

Thanks!

like image 344
user1758367 Avatar asked Dec 10 '12 11:12

user1758367


2 Answers

Consider Getopt::Long plus Pod::Usage. My usual pattern for writing CLI tools:

#!/usr/bin/env perl
# ABSTRACT: Short tool description
# PODNAME: toolname
use autodie;
use strict;
use utf8;
use warnings qw(all);

use Getopt::Long;
use Pod::Usage;

# VERSION

=head1 SYNOPSIS

    toolname [options] files

=head1 DESCRIPTION

...

=cut

GetOptions(
    q(help)             => \my $help,
    q(verbose)          => \my $verbose,
) or pod2usage(q(-verbose) => 1);
pod2usage(q(-verbose) => 1) if $help;

# Actual code below
like image 107
creaktive Avatar answered Sep 20 '22 18:09

creaktive


easy to use this :

if( $ARGV[0] eq '-h' || $ARGV[0] eq '-help')
{
help();
exit;
}


sub help { print "My help blah blah blah\n";
}
like image 27
starinsky Avatar answered Sep 17 '22 18:09

starinsky