Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Parsing command line options

1,446 views
Skip to first unread message

hemant.b...@gmail.com

unread,
Sep 7, 2012, 12:01:20 PM9/7/12
to
Hi Tcl-ites,

I am wondering if there is any way to parse command line options in tcl? I have command as:
./script.tcl -file <log_file> -status <status>

I see that we can do it using 'package require cmdline' but what in case i do have this package installed in my machine..


Perl programmers,
I usually do it using getopts in perl.


Thanks in advance..

- Hemant

Gerry Snyder

unread,
Sep 7, 2012, 12:08:20 PM9/7/12
to
On 9/7/2012 9:01 AM, hemant.b...@gmail.com wrote:
> Hi Tcl-ites,
>
> I am wondering if there is any way to parse command line options in tcl? I have command as:
> ./script.tcl -file <log_file> -status <status>

Hemant,

Check out http://wiki.tcl.tk/10702 for some answers.

HTH,

Gerry

hemant.b...@gmail.com

unread,
Sep 7, 2012, 1:26:40 PM9/7/12
to
Thanks Gerry.. It was helpful.. :)

Les Cargill

unread,
Sep 7, 2012, 9:59:44 PM9/7/12
to
hemant.b...@gmail.com wrote:
> Hi Tcl-ites,
>
> I am wondering if there is any way to parse command line options in tcl? I have command as:
> ./script.tcl -file <log_file> -status <status>
>
C:\c\usenet>cat args.tcl


array set options $::argv

parray options

C:\c\usenet>tclsh85 args.tcl -foo bar -fee fi
options(-fee) = fi
options(-foo) = bar

C:\c\usenet>


> I see that we can do it using 'package require cmdline' but what in case i do have this package installed in my machine..
>
>
> Perl programmers,
> I usually do it using getopts in perl.
>
>
> Thanks in advance..
>
> - Hemant
>

--
Les Cargill

hemant.b...@gmail.com

unread,
Sep 10, 2012, 5:15:27 AM9/10/12
to
Hi Les,

Thanks for your reply..

I see what you are doing. You are mapping command lines arguments into an associative array.

Question,
How do i get these variables into script itself? I tried below code but that does not seem to be worked out for me.

array set options $::argv
parray options ;# Printing into the standard output
set golden $options("gold")
set revised $options("rev")
set exact $options("exact")


puts "Golden: $golden\n";
puts "Revised: $revised\n";

Again.. Thanks.
- Hemant

Eugene

unread,
Sep 10, 2012, 5:32:45 AM9/10/12
to
Hi,
Try losing the quotes around array keys, like this:

set golden $options(gold); # instead of "gold"

Tcl tries to find the _exact_ value of the key, in your case its "gold"
and if you call your script this way: tclsh script.tcl gold value -
there's no "gold" option ;)

--
Best regards, Eugene.

Les Cargill

unread,
Sep 10, 2012, 8:43:02 AM9/10/12
to
hemant.b...@gmail.com wrote:
> Hi Les,
>
> Thanks for your reply..
>
> I see what you are doing. You are mapping command lines arguments into an associative array.
>
> Question,
> How do i get these variables into script itself? I tried below code but that does not seem to be worked out for me.
>
> array set options $::argv
> parray options ;# Printing into the standard output
> set golden $options("gold")
> set revised $options("rev")
> set exact $options("exact")
>
>
> puts "Golden: $golden\n";
> puts "Revised: $revised\n";
>
> Again.. Thanks.
> - Hemant
>
>


See Eugene's reply - he's got it. Lose the " marks.

In Tcl there is a risk of "quoting hell". You have to know
where you are to know when things are substituted and how:

http://wiki.tcl.tk/1726

Also, had you designed this to use -gold ... -rev ... -exact, it's
easier to see what is an option name and what is a value for the
option on a command line. You can always strip the dash characters
after.


>>
>>
>> array set options $::argv
>>
>>
>>
>> parray options
>>
>>
>>
>> C:\c\usenet>tclsh85 args.tcl -foo bar -fee fi
>>
>> options(-fee) = fi
>>
>> options(-foo) = bar
>>

--
Les Cargill

0 new messages