CC: | Adam Pfeiffer <apfeiffe [...] Brocade.com> |
Subject: | RFE request - Sort output of print_usage by required, optional and add new sortby => name |
Date: | Thu, 9 Apr 2015 23:47:33 +0000 |
To: | "bug-Getopt-ArgParse [...] rt.cpan.org" <bug-Getopt-ArgParse [...] rt.cpan.org> |
From: | Adam Pfeiffer <apfeiffe [...] Brocade.com> |
Hello,
Currently, with this script:
####
1 use Getopt::ArgParse;
2
3 my $ap = Getopt::ArgParse->new_parser(
4 prog => "$0",
5 description => "Description goes here",
6 sortby => 'position'
7 );
8
9 $ap->add_arg('--required', help => 'required param',required =>1);
10 $ap->add_arg('--optional', help => 'optional param');
11 $ap->add_arg('--c', help => 'optional param');
12 $ap->add_arg('--b', help => 'optional param', required => 1);
13 $ap->add_arg('--a', help => 'optional param');
14
15 eval
16 {
17 $ns = $ap->parse_args()
18 };
19 if ($@)
20 {
21 my $error = $@;
22 print $ap->print_usage() . "\n";
23 print "$error" if ($error);
24 exit;
25 } ####
Will produce this output with the -h option:
####
usage: argparse_script.pl [--help|-h] --required [--optional] [--c] --b [--a]
Description goes here
required named arguments:
--required REQUIRED required param
--b B optional param
optional named arguments:
--help, -h ? show this help message and exit
--optional OPTIONAL ? optional param
--c C ? optional param
--a A ? optional param
####
In this output, the required parameters -required and -b are not grouped. The required parameter -b is easy to miss in the usage statement. I believe that all of the required parameters should be listed first in the usage followed by the optional arguments (with help at the front of the optional arguments) so usage would look like this:
####
argparse_script.pl --b --required [--help|-h] [--a] [--c] [--optional]
####
I have created a pull request in github with the above changes incorporated into the code.
I also added a parameter to new_parser called sortby. This parameter is not required, defaults to 'position' and if passed in must be either 'name', or 'position'.
If you set sortby to 'name' it will sort by group (required then optional) and inside each group it will sort the options by name so the output would look like this:
####
usage: argparse_script.pl --b --required [--help|-h] [--a] [--c] [--optional]
Description goes here
required named arguments:
--b B optional param
--required REQUIRED required param
optional named arguments:
--help, -h ? show this help message and exit
--a A ? optional param
--c C ? optional param
--optional OPTIONAL ? optional param
####
In the same github pull request I have included the code to enable sorting by name.
Please take a look at my changes and let me know your thoughts on the change.
Thanks much
Adam Pfeiffer
SQA Team
Brocade
4 Brocade Parkway, Broomfield, CO 80021
(720) 558-3681
www.brocade.com<http://www.brocade.com/>
[Brocade Logo]
Message body is not shown because it is too large.