slang-users mailing list

[2015 Date Index] [2015 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] An S-Lang script


Morten Bo Johansen <mbj@xxxxxxxxxxx> wrote:
> Just in case you had one or two minutes to quickly look it over
> and offer your comments, corrections or suggestions pertaining
> to anything from style to (especially) speed/efficiency - and

I would add

   if (__argc == 1) usage ();

at the beginning of slsh_main.

It seems to me that the general interface is something like:

  slpo ACTION <args> [-o outfile]

If this is correct, then I would change the usage message to look like:

  slpo [options] ACTION <args>

where options is one of: -h|--help|-v|--version|-o|--outputfile
You can use slcmd to process these options.

Then I would use a table to process the rest of the arguments.

As an example, look at the following toy script.  The first few
functions that call "print_args" are simply there to show that the
correct calls are made. You can run it using:

   slsh t.sl diff a b
   slsh t.sl spellcheck file
   slsh t.sl -o outfile spellcheck-list file
      .
      .

I hope this helps.
Thanks,
--John

require ("cmdopt");

define print_args (name, nargs)
{
   () = fprintf (stdout, "%s", name);
   variable args = __pop_list(nargs);
   foreach (args)
     {
	variable arg = ();
	() = fprintf (stdout, " %S", arg);
     }
   () = fputs ("\n", stdout);
   return NULL;
}

define spellcheck ()
{
   print_args ("spellcheck", _NARGS);
}
define po_diff ()
{
   print_args ("diff", _NARGS);
}
define print_version_and_exit ()
{
   print_args ("print_version_and_exit", _NARGS);
   exit (1);
}
define print_help_and_exit ()
{
   print_args ("print_version_and_exit", _NARGS);
   exit (1);
}
define write_string_to_file ()
{
   print_args ("write_string_to_file", _NARGS);
}

private variable Action_Type = struct
{
   function,
   min_args,
   max_args,
};

private variable Action_Table = Assoc_Type[];

private define add_action (names, function, min_args, max_args)
{
   variable s = @Action_Type;
   s.function = function;
   s.min_args = min_args;
   s.max_args = max_args;

   foreach (names)
     {
	variable name = ();
	Action_Table[name] = s;
     }
}

private define do_spellcheck (name, args)
{
   return spellcheck (args[0], 1);
}
add_action ({"sl", "spellcheck"}, &do_spellcheck, 1, 1);

private define do_spellchecklist (name, args)
{
   return spellcheck (args[0], 0);
}
add_action ({"sl", "spellcheck-list"}, &do_spellchecklist, 1, 1);

private define do_diff (name, args)
{
   return po_diff (args[0], args[1]);
}
add_action ({"d","diff"}, &do_diff, 2, 2);

define slsh_main ()
{
   % Use cmdopt to process options
   variable outfile = NULL;
   variable c = cmdopt_new ();
   c.add ("v|version", &print_version_and_exit);
   c.add ("h|help", &print_help_and_exit);
   c.add ("o|--output-file", &outfile; type="str");

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

   variable action = __argv[i];
   variable action_args = __argv[[i+1:]];

   ifnot (assoc_key_exists (Action_Table, action))
     {
	() = fprintf (stderr, "Unsupported action: %s\n", action);
	exit (1);
     }
   variable s = Action_Table[action];
   ifnot (s.min_args <= length(action_args) <= s.max_args)
     {
	() = fprintf (stderr, "Expecting %d - %d args for action %s\n",
		      s.min_args, s.max_args, action);
	exit (1);
     }

   variable out = (@s.function) (action, action_args);
   if (out == NULL)
     return;

   if (outfile == NULL)
     () = fprintf (stdout, "%s\n", out);
   else
     write_string_to_file (out, outfile);
}
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


[2015 date index] [2015 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]