Processing command line. 
Author Message
 Processing command line.

Hi All,

I wish to have my program accept options like so :

myprog --start something

or

myprog --stop something

Now I am having problems extracting either --start and --stop or
"something". I am using Getopt::Long to do the processing. Here is an
example of code where I can get the "something" but not "--start" or
"--stop" :

use Getopt::Long;

$success = GetOptions (
        "start=s" => \$start,
        "stop=s" => \$stop
        );

This will assign the value of "something" to $start or $stop. But now I
dont know whether to start or stop the "something" because I only have
"something" on $start or $stop.

Here is an example of code where I can find out whether to start or stop
but I cannot get the value "something" assigned :

use Getopt::Long;

$success = GetOptions (
       "start=s" => sub { $operation = "start" },
       "stop=s" => sub { $operation = "stop" }
);

Now I know that I must where I must start or stop "something" but I have no
way of assigning "something" to a variable. Is there a way of both setting
a flag ($operation in this case) and getting the value of what just
followed that flag. I guess it boils down to : how to I extract bare
options not preceeded by anything?

I could just do this :

myprog --start  --prog something

But I would really like the behaviour mentioned in the first example.

Any help would be appreciated.

Kind regards,
Fred
--
Remove nospam to get real address.



Mon, 16 May 2005 17:33:21 GMT  
 Processing command line.

Quote:

> Hi All,

Hi,

Quote:
> [sniped]
> Here is an example of code where I can find out whether to start or stop
> but I cannot get the value "something" assigned :

> use Getopt::Long;

> $success = GetOptions (
>        "start=s" => sub { $operation = "start" },
>        "stop=s" => sub { $operation = "stop" }
> );


Doing that, you keep start or stop and you obtain the "something" value(s)

Hope this helps.

Sebastien



Mon, 16 May 2005 17:41:44 GMT  
 Processing command line.

Quote:

> Hi All,

> I wish to have my program accept options like so :

> myprog --start something

> or

> myprog --stop something

> Now I am having problems extracting either --start and --stop or
> "something". I am using Getopt::Long to do the processing. Here is an
> example of code where I can get the "something" but not "--start" or
> "--stop" :

> use Getopt::Long;

> $success = GetOptions (
>         "start=s" => \$start,
>         "stop=s" => \$stop
>         );

> This will assign the value of "something" to $start or $stop. But now I
> dont know whether to start or stop the "something" because I only have
> "something" on $start or $stop.

Hi,

near the end of the Getopt::Long documentation, there is
an example of storing the GetOptions-results into a hash,
which might do what you want.

I tried this snippet:
------------------------------------------------------
#!/usr/bin/perl -w

use Getopt::Long;
my %hash;

my $success = GetOptions ( \%hash, 'start=s', 'stop=s' );
print "$_ -> $hash{$_}\n" foreach( keys %hash );
------------------------------------------------------
and called it like
$ myprog --start application
which gave me
start -> application

You can test with exists() on the hash for which
action was intended. Still keeping in the eye that this makes
it possible to enter something like
$ myprog --start one --stop other

HTH
-Christian



Mon, 16 May 2005 18:14:34 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Processing command line

2. ANNOUNCE: Getopt::Long 2.9 -- Universal command line options processing

3. HELP: Command line processing

4. patch1 for wafeperl 0.2 to allow xt command line argument processing

5. ANNOUNCE: Getopt::Long 2.6 -- Universal command line options processing

6. Perl command line processing, Windows/dos style ?

7. HELP: Command line processing

8. basic query about command line processing in Perl

9. ANNOUNCE: Getopt::Long 2.9 -- Universal command line options processing

10. ANNOUNCE: Getopt::Long 2.6 -- Universal command line options processing

11. command line processing

12. Simple I/O to a command-line process?

 

 
Powered by phpBB® Forum Software