-help parm

49 views
Skip to first unread message

Ard Vilken

unread,
Jan 10, 2023, 4:25:50 PMJan 10
to


Trying to allow a tk script allow a user parm of "-help". It looks like tcl has no problems with it but if using /usr/bin/wish in linux, the "-help" command line parm is immediately getting picked up by the interpreter and not by the standard user args in argv/argc and it prints some basic wish options to the terminal.

I don't want to change the "-help" option as I want the user to have a standard way of asking and it's used 50 other places.

Shaun Deacon

unread,
Jan 10, 2023, 5:35:40 PMJan 10
to
On Tuesday, January 10, 2023 at 1:25:50 PM UTC-8, ardv...@gmail.com wrote:
> Trying to allow a tk script allow a user parm of "-help". It looks like tcl has no problems with it but if using /usr/bin/wish in linux, the "-help" command line parm is immediately getting picked up by the interpreter and not by the standard user args in argv/argc and it prints some basic wish options to the terminal.
>
> I don't want to change the "-help" option as I want the user to have a standard way of asking and it's used 50 other places.

You should insert a "--" argument just before your script arguments. This stops wish from interpreting your script arguments and passes them through to the script's argv variable.

https://www.tcl.tk/man/tcl/UserCmd/wish.html

Shaun

Schelte

unread,
Jan 10, 2023, 5:41:46 PMJan 10
to
On 10/01/2023 22:25, Ard Vilken wrote:
>
>
> Trying to allow a tk script allow a user parm of "-help". It looks like tcl has no problems with it but if using /usr/bin/wish in linux, the "-help" command line parm is immediately getting picked up by the interpreter and not by the standard user args in argv/argc and it prints some basic wish options to the terminal.
>
> I don't want to change the "-help" option as I want the user to have a standard way of asking and it's used 50 other places.

Run your script with tclsh instead of wish. Process $argv, or copy it to
another variable. Then clear argv and do a [package require Tk].


Schelte.


Shaun Deacon

unread,
Jan 10, 2023, 7:13:24 PMJan 10
to
On Tuesday, January 10, 2023 at 2:41:46 PM UTC-8, Schelte wrote:
> > I don't want to change the "-help" option as I want the user to have a standard way of asking and it's used 50 other places.
> Run your script with tclsh instead of wish. Process $argv, or copy it to
> another variable. Then clear argv and do a [package require Tk].
>
> Schelte.

Will work of course, but personally I still think it's easier to just use '--' if the OP wants to stay with wish...

Example "args.tcl" :
foreach arg $argv { puts "my arg = $arg" }

%> wish args.tcl -help
Application initialization failed: Command-specific options:
-colormap: Colormap for main window
-display: Display to use
-geometry: Initial geometry for window
-name: Name to use for application
-sync: Use synchronous mode for display server
-visual: Visual for main window
-use: Id of window in which to embed application
--: Pass all remaining arguments through to script
my arg = -help

%> wish args.tcl -- -help
my arg = -help

First invocation causes an error because '-help' is not a wish argument (so no Tk window is displayed) but runs the script (print argv contents)
Second invocation opens a Tk window, passes -help to argv, and runs the script (print argv contents)

Shaun

Ard Vilken

unread,
Jan 10, 2023, 9:16:48 PMJan 10
to
I was hoping, from a user training POV not to use --help and I can take a look Schelte's approach. You were able to confirm that I wasn't doing something wrong. Thanks for the quick answers.

saitology9

unread,
Jan 10, 2023, 10:29:46 PMJan 10
to
On 1/10/2023 9:16 PM, Ard Vilken wrote:
> I was hoping, from a user training POV not to use --help and I can take a look Schelte's approach. You were able to confirm that I wasn't doing something wrong. Thanks for the quick answers.


The proper solution here is to use "--" before listing your options for
your own script.

Using "--" as an option itself to prevent the command line from
consuming other options is a pretty standard practice. Some Tcl
commands like "switch" also use it to avoid interpreting certain data
items (ie. those starting with a dash) as potential command options. It
is a safe bet to say that anyone using Linux knows about this convention
too.

Also keep in mind that "-help" is a common option across many
commands/programs. It was accepted and gave you an answer (albeit a
wrong one). Otherwise, you could have gotten an error message, if for
example, it was named "-displayhelp".

Alex P

unread,
Jan 11, 2023, 1:27:41 AMJan 11
to
Schelte is right. You should also remember about ::argc variable that is length of ::argv and must be modified along with ::argv, e.g. this way:

puts "$::argc - $::argv" ;# to test and drop
set i [lsearch $::argv -help]
if {$i>-1} {
puts {
Its' my help:
Do this and that...
}
set ::argv [lreplace $::argv $i $i]
set ::argc [llength $::argv]
}
unset i
package require Tk
puts "$::argc - $::argv" ;# to test and drop
Reply all
Reply to author
Forward
0 new messages