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

I can't get slsh to take arguments with scripts properly.

4 views
Skip to first unread message

ShadowTek

unread,
Jul 30, 2009, 4:16:55 PM7/30/09
to
I'm tryin to get slsh to load a scripts with arguments so that I can try
to learn slang, but I can't seem to pass arguments properly through slsh.

Heres an example script, temp.sl:
==============
#!/usr/bin/env slsh

define slsh_main ()
{
message (string (__argc));
return;
}
=============

When I try "slsh temp.sl", the result is "1".
When I try "slsh temp.sl 8", the result is "2".
The result is also "2" regardless of any number I try.

What am I doing wrong?

I also tried sprintf, but the results where the same.

John E. Davis

unread,
Jul 31, 2009, 4:17:03 PM7/31/09
to
On Thu, 30 Jul 2009 20:16:55 +0000 (UTC), ShadowTek <Shad...@invalid.invalid>
wrote:

> I'm tryin to get slsh to load a scripts with arguments so that I can try
> to learn slang, but I can't seem to pass arguments properly through slsh.
>
> Heres an example script, temp.sl:
>==============
> #!/usr/bin/env slsh
>
> define slsh_main ()
>{
> message (string (__argc));
> return;
>}
>=============
>
> When I try "slsh temp.sl", the result is "1".
> When I try "slsh temp.sl 8", the result is "2".
> The result is also "2" regardless of any number I try.
>
> What am I doing wrong?

__argc gives the number of command line arguments passed to the
script, with the name of the script counted as the first argument.

Try this:

define slsh_main ()
{
variable i;
for (i = 0; i < __argc; i++)
vmessage ("argv[%d] = %s", i, __argv[i]);
}

FWIW, here is a template that I use for my slsh scripts:

#!/usr/bin/env slsh

private variable Script_Version_String = "0.1.0";

require ("cmdopt");

private define exit_version ()
{
() = fprintf (stdout, "Version: %S\n", Script_Version_String);
exit (0);
}

private define exit_usage ()
{
variable fp = stderr;
() = fprintf (fp, "Usage: %s [options] REQUIRED-ARGS\n", __argv[0]);
variable opts =
[
"Options:\n",
" -v|--version Print version\n",
" -h|--help This message\n",
];
foreach (opts)
{
variable opt = ();
() = fputs (opt, fp);
}
exit (1);
}


define slsh_main ()
{
variable c = cmdopt_new ();

c.add("h|help", &exit_usage);
c.add("v|version", &exit_version);

variable i = c.process (__argv, 1);

% modify the next line to account for the number of required
% arguments
if (i != __argc)
exit_usage ();

% Rest of the code goes here.
}

Good luck,
--John

ShadowTek

unread,
Aug 1, 2009, 11:27:34 PM8/1/09
to
On 2009-07-31, John E. Davis <j...@jedsoft.org> wrote:

> __argc gives the number of command line arguments passed to the
> script, with the name of the script counted as the first argument.

Oh, I didn't realise that they were arrays. I just saw "__argc" in the
introductory documentation and that's what I tried.

Thanks.

John E. Davis

unread,
Aug 2, 2009, 12:04:20 AM8/2/09
to
On Sun, 2 Aug 2009 03:27:34 +0000 (UTC), ShadowTek <Shad...@invalid.invalid>

> Oh, I didn't realise that they were arrays. I just saw "__argc" in the
> introductory documentation and that's what I tried.

You will find some documentation on command line processing by slsh
scripts at <http://www.jedsoft.org/slang/doc/html/slang.html#toc19.2>.
--John

0 new messages