Hi Perlmonks I am trying to get my head around using getopt::long and inparticular how excute a subroutine depending on if a certain command line option has a value assigned. I have read through the perl documentation re using getopt::long but I can't find any example of how to do this so I was hoping you might be able to help me please. Below is the code I have written so far:
#!/bbs/opt/bin/csperl5.12 use strict; no strict "refs"; use warnings; use File::Compare; use File::Basename; use IO::File; use Sys::Hostname; use Data::Dumper; use Tie::File; use Encode; use lib "/bbsrc/bin/prod/lib/site_perl"; use JSON::XS qw( decode_json ); use Getopt::Long; sub usage { my $prog = basename $0; print <<_USAGE_END; Usage:: $prog -h | <tag name> | [-v] [ --validate] <Json cfg locat +ion> <Output file location> --verbose | -v Run tool in verbose mode. --help | -h Usage information. --validate | -V Validate Json object and Validate this again +st Json schema. --conf | -c Json object file location --output | -o Output location for nfs mount report --log | -l Output logfile location. --tag | -t Generate output file for all nfs mounts for a pa +rticular tag. --mount | -m Specify mountpoint name for example software, +tools etc: This will generate an output file detailing all mounts tha +t are required for the current server this tool is being executed on. --host | -H Specify a server hostname and this tool will gen +erate a report detailing every single nfs mount that is required on t +hat particular server. _USAGE_END } sub getoptions { my %opts = qw( verbose 0 ); Getopt::Long::Configure("bundling"); unless ( GetOptions ( \%opts, qw[ verbose|v help|h validate|V conf|c=s output|o=s log|l=s tag|t=s mount|m=s host|H=s ] ) ) { usage(); exit 1; } usage(), exit 0 if $opts{help}; if (!@ARGV) { print "Error:: No arguments specified to $0.\n"; usage(); exit 1; } return \%opts; } #----------------------------------------------------------- MAIN: { my $opts = getoptions(); }
What I am trying to do is if the command line option --conf is specified then I want to set the value of this to $master_conf (this config file is json data that I am decoding). I have the following subroutine which I want to use to decocde this config file but I am not too sure how to set the value of --conf to $master_conf so I can then call this subroutine. Please can you help me on what I need to do here?
sub extract_json { my $file = shift; local $/; #enable slurp open my $fh, "<", "$file"; my $json = <$fh>; return $json; } my $json_data = extract_json("$master_conf"); my $decoded_json_obj = decode_json( $json_data);
In reply to Using getopt::long and how to use the value of one of the command line options in a subroutine by NewLondonPerl1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |