- Subject: Re: [slang-users] cmdopt
- From: jed@xxxxxxxxxxx (John E. Davis)
- Date: Tue, 11 Aug 2015 18:23:15 -0400
Morten Bo Johansen <mbj@xxxxxxxxxxx> wrote:
> #!/usr/bin/env slsh
>
> _debug_info=1;_traceback=1;_slangtrace=1;
You do not need the last line. You can run the script using
`slsh -g script args...` to cause it to print debug info. However, it
would be better to use sldb to debug it: sldb script args....
> require ("cmdopt");
[...]
> define slsh_main ()
>{
> variable c = cmdopt_new ();
> cmdopt_add (c, "a", &p, __argv[2]; type="str");
You will get an index range error if you have not passed at least 2
arguments to the script. The index error that you see comes from the
line below since you have not passed at least 4 other command line
arguments.
> cmdopt_add (c, "b", &pp, __argv[2], __argv[3], __argv[4]; type="str");
In both cases, you are incorrectly using the cmdopt_add function.
It seems that you are trying to invoke a callback function to handle a
variable number of command-line arguments. Like the getopt function,
this module only supports command line switches that have no optional
value or those that accept a single value.
I modified your code so that the following happens:
$ ./t.sl
Unhandled arguments:
$ ./t.sl -a 1 -b 2 3 4
1
2
Unhandled arguments:
argv[5] = 3
argv[6] = 4
$ ./t.sl -a 1 -b 2 -b 3 4
1
23
Unhandled arguments:
argv[7] = 4
$ ./t.sl -a 1 -b A -b B -b C foo
1
ABC
Unhandled arguments:
argv[9] = foo
Here is the script. I hope this helps. Thanks, --John
#!/usr/bin/env slsh
require ("cmdopt");
private define p (a)
{
() = printf ("%s\n", a);
}
private define print_list (list)
{
() = printf ("%s\n", strcat (__push_list (list)));
}
define slsh_main ()
{
variable list = {};
variable c = cmdopt_new ();
c.add ("a", &p; type="str");
c.add ("b", &list; append, type="str");
variable i = cmdopt_process (c, __argv, 1);
if (length (list))
print_list (list);
message ("Unhandled arguments:");
while (i < __argc)
{
vmessage (" argv[%d] = %s", i, __argv[i]);
i++;
}
}
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.
[2015 date index]
[2015 thread index]
[Thread Prev] [Thread Next]
[Date Prev] [Date Next]