• -help parm

    From Ard Vilken@21:1/5 to All on Tue Jan 10 13:25:47 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Schelte@21:1/5 to Ard Vilken on Tue Jan 10 23:41:40 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Shaun Deacon@21:1/5 to [email protected] on Tue Jan 10 14:35:37 2023
    On Tuesday, January 10, 2023 at 1:25:50 PM UTC-8, [email protected] 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Shaun Deacon@21:1/5 to Schelte on Tue Jan 10 16:13:21 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ard Vilken@21:1/5 to All on Tue Jan 10 18:16:46 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From saitology9@21:1/5 to Ard Vilken on Tue Jan 10 22:29:40 2023
    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".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alex P@21:1/5 to All on Tue Jan 10 22:27:39 2023
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)