argv

Difference between version 62 and 64 - Previous - Next
'''`$argv`''' is a global variable provided by [tclsh] and [wish] mainline code
(and '''NOT''' the tcl or tk libraries). 



** See Also **

   [command options]:   



** Description **

`$argv` contains the arguments the app was called with as a list, similar to
C's argv, but without the app name itself, which is stored in `$::[argv0]`.
While ''[argc]'' exists, we don't really need it since we can always determine
[[llength $argv]]. 

An example of making use of this variable is:

======
#! /usr/local/bin/tclsh
if { $::argc > 0 } {
    set i 1
    foreach arg $::argv {
        puts "argument $i is $arg"
        incr i
    }
} else {
    puts "no command line argument passed"
}
======


[DGP]:  It is true that we do not need ''::argc'', but '''tclsh''' and
'''wish''' and any application embedding Tcl that calls ''Tcl_Main()'' provide
it anyway. Also, new child interpreters don't get [argc] and [argv] set by
default - set them if they need them.

[argv0] describes the name of the top-level script that was invoked via tclsh
or wish.

[http://www.tcl.tk/man/tcl/UserCmd/tclsh.htm%|%tclsh man page]:   
[http://www.tcl.tk/man/tcl/UserCmd/wish.htm%|%wish man page]:   

Tk's Tk_ArgVInfo function makes a pass over the argv items, and handles some
more arguments before placing the remainder in the argv list..

 
The arguments that these functions look for include:

   `-help`:   print out a usage statment
   `-colormap`:   specifies that the window should have a new private colormap
   `-display`:   specify the display and screen to use
   `-file`:   Ignored
   `-geometry`:   specify the location and size of the window
   `-name`:   used as title of the window and name of the interpreter
   `-sync`:   synchronous X server commands
   `-use`:   the main window of this app is to be embedded in another window
   `-visual`:   specify which visual to use for the window.


This means that you should not design your application to require flags of
these names or even abbreviations of these!  Otherwise, The argvinfo function
in Tk grabs them before your application finds them.

[RS] 2013-10-08 - If you don't need the Tk options, but want to preserve your argv (where maybe -n xx would be consumed as matching -name), in scripts with the [tclsh] executable it helps to do
 set argv [linsert $argv 0 --]
 package require Tk
When [wish] is the executable, you can only write the "--" flag explicitly, or define a shell alias:
 $ alias wish='wish --'

----

See [command options] for a discussion of various options one has in parsing
the argv (and argv-like) information.

Also, note that [[[info script]]] describes the name of the currently-executing
script and this name even gets set by [source].  ''Technically, it is set by
Tcl_FSEvalFile(), for those using Tcl's C API.''

The command [[[info nameofexecutable]]] describes the name of the executing
binary, and might be thought more like the C level argv[[0]].


Some code available that helps process the argv arguments include:

   * [clig (Kirsch)]
   * [CmdLine]

   * [evaluate_parameters]
 Where: ftp://ftp.Lehigh.EDU/pub/evap/evap-2.x/evap-2.2.tar.Z        ftp://ftp.tcl.tk/pub/tcl/mirror/ftp.procplace.com/pub/tcl/sorted/packages-7.6/devel/evap-2.1.tar.gz
 Description: evaluate_parameters is a Perl program that
        processes command line arguments in a simple consistent manner
        performing type-checking, multi-levels of help, etc.
        generate_tk_program is an auxiliary program which generates
        a Tcl/Tk GUI wrapper around one's Perl or C program to gather
        the command line arguments interactively.
 Updated: 10/1999
 Contact: mailto:[email protected] (Stephen O. Lidie)

   * [getopt]
   * [ParseArgs]
   * [argp]
   * [argument parsing with defaults (Wagner)]
   * [argument processing for Tcl (Lehenbauer)]
   * [argument processing (Zimmer)]

   * [The Simple Development Library] [http://simpledevlib.sourceforge.net/] includes both a package for command arguments handling including typed arguments (with optional run-time checking) and options (SimpleProc, [http://simpledevlib.sourceforge.net/SimpleProc.html]) and a very powerful command line options parser with too many features to list here  (SimpleOption, [http://simpledevlib.sourceforge.net/SimpleOption.html]).

   * sneaky_eval[http://tcltk.free.fr/index.php3?idxcat=100&page=21]
 Where: From the contact
 Description: Preprocess arguments before calling RecordAndEval or Eval,
        surrounding the arguments with braces so they won't be sub-evaluated.
 Updated:
 Contact: mailto:[email protected] (Matt DiMeo)

   * SNTL[http://www.csua.berkeley.edu/%7Esls/woa/distrib/]
 Where: http://www.csua.berkeley.edu/%7Esls/woa/distrib/        ftp://ftp.tcl.tk/pub/tcl/mirror/ftp.procplace.com/pub/tcl/sorted/packages-7.6/devel/sntl-0.4.2.tar.gz
        ftp://ftp.tcl.tk/pub/tcl/mirror/ftp.procplace.com/pub/tcl/sorted/packages-7.6/devel/sntl-0.4.2p1.patch.tar.gz
 Description: A general Tcl library of procedures.  Contains code to
        produce man pages from Tcl source, conversions from Tcl to C,
        HTML rendering, generating HTML, handling CGI forms, command line
        argument processing, a debugging message system, an object system,
        and various Tk widgets built with the object system.
 Updated: 10/1998
 Contact: mailto:[email protected] (Sam Shen)
        mailto:[email protected] (Sam Shen)

   * [variable argument process]
   * [yaap - Yet Another Argument Parsing utility]
   * [command options]

----

Schelte Bron wrote in [comp.lang.tcl] on 2004-02-19: 

''I sometimes use numbers as variable names for a list of arguments like argv
in the main script or args inside a proc.''

======
set i 0; foreach n $argv {set [incr i] $n}
======

''I can then refer to the arguments as $1, $2, etc. just like in sh/ksh/bash.''

[rdt] Uhh, can't you just have the proc use those name?  i.e.

======
proc xyz {1 2 3} {
    # use the args as:
    puts "1=$1, 2=$2, 3=$3"
}
======

What's wrong with that if that is what you want?

[sbron]:  There's nothing wrong with that, in a different situation.  The point
was having easy access to an unknown number of arguments.

[rdt] I see.  Yes that is additional capability, thanks.

[MG]:  adds that (especially when you're doing it outside a proc, and so the
variables are all persistant) you should add an 'unset i n' to the end of the
code above, just to clean up properly.
----

BZZZZ:  Is there a way to get binary argv? `argv` parsing UTF-8 can create equal tcl strings for non-equal arguments:
======
#!/usr/bin/bash
echo 'puts [string equal {*}$argv]' > are_2_args_equal.tcl
tclsh are_2_args_equal.tcl $'\xC3\xAE' $'\xEE'
======
outputs
======
1
======
but `$'\xC3\xAE'` and `$'\xEE'` are different

<<categories>> Tcl syntax | Category Argument Processing | Category Internals | Arts and Crafts of Tcl-Tk Programming