pupa-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Argument parser


From: Marco Gerards
Subject: Re: Argument parser
Date: 15 Dec 2003 22:51:20 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

"Yoshinori K. Okuji" <address@hidden> writes:

> On Sunday 14 December 2003 02:14, Marco Gerards wrote:
> > > Isn't it difficult? For example, if you define a command which
> > > takes two essential arguments, how do you implement this command
> > > with your parser?
> >
> > In the structure that is used to store the state of the parser you
> > have to include a counter, for example:
> 
> A counter is not enough. You need complete information, but not only the 
> number of arguments. Consider "ls foo -l bar -h baz".

It is enough.  The counter is just used to see if the maximum amount
of arguments has not been reached yet.  In case of your example the
following calls will happen:

option_parser (PUPA_KEY_ARG, "foo", 0x12345);
option_parser ('l', 0, 0x12345);
option_parser (PUPA_KEY_ARG, "bar", 0x12345);
option_parser ('h', 0, 0x12345);
option_parser (PUPA_KEY_ARG, "baz", 0x12345);

In case the option "--foo=BAR" would be recognized and "--foo=abc" (-f
for short here) would have been given the following would have been
called:

option_parser ('f', "abc", 0x12345);

After parsing the parser will call:
option_parser (PUPA_KEY_END, 0, 0x12345);

Using this the option_parser function can return an error if the
amount of arguments was incorrect.

I hope this simplistic example gives you more insight on how it will
work.  I do not understand what other information you were talking
about buy I have showed you here you get all information there is.

> > > It is easy until you start implementing the real function of ls.
> > > This is the same question above, but how do you separate an option
> > > parser from the real function completely? Using a linked list to
> > > store arguments?
> >
> > Possibly.  The function will be called for every argument.  If you
> > would like to see an example I can write one.
> 
> It's too bad. It's far from simple. If you have an array, you can just 
> do "argv[0]" and "argv[1]".

Not always.  In your example the arguments (short, long and other)
age mixed up.  In that case you cannot use an array.

Of course it is easy to make an array of the arguments that are not a
short/long option.  This array can be passed to the command as
well. So a command can look like this:

int pupa_cmd_ls (..., char *args[]);

With ... it passes the state of the parser.  Perhaps the args can be
put into the parser state too, I didn't think about this yet.

> > > Ummh, you are right, ls uses -h for another purpose. But I don't
> > > think this is so good.
> >
> > Why don't you like it?  The '-?' short option is already used by
> > other GNU programs too, I don't remember GNU programs using '-h' for
> > help, more often '-h' is used for other purposes.  AFAIK --help
> > doesn't even have a short option.
> 
> Because -? is difficult to input in reality. If you implement shell 
> wildcards, you must input it as "-\?", and backslash is really 
> difficult to input in many keyboards.

Right.  This is a good point.

> > This means that it should be possible for PUPA to store stuff on
> > disk.  This should not be hard to do, but this is something we need
> > to discuss.  The best thing to me seems pre-allocating some blocks on
> > disk (by using a file of course) and reading and writing to this
> > file.
> 
> It can be implemented as "simple mathematics + savedefault", so you 
> don't need any new feature actually.

Ok.  I will have a look at how savedefault works.

[...]

Thanks,
Marco





reply via email to

[Prev in Thread] Current Thread [Next in Thread]