help-gengetopt
[Top][All Lists]
Advanced

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

Re: [help-gengetopt] make cmd_line_list and cmd_line_list_tmp static?


From: Andre Noll
Subject: Re: [help-gengetopt] make cmd_line_list and cmd_line_list_tmp static?
Date: Mon, 20 Feb 2006 04:17:19 +0100
User-agent: Mutt/1.5.9i

Hi Lorenzo,

On 12:37, Lorenzo Bettini wrote:
> I've uploaded a candidate 2.16 release with this feature:
> 
> http://rap.dsi.unifi.it/~bettini/gengetopt-2.16-rc.tar.gz
> 
> could you please try it and tell me if this satisifies your request?

Yes, works fine for me; I just got rid of the ugly sed-script which did
the conversion optind=1 -> optind=0 :)

Thank you very much again.

> by the way, could you please send me some code snippet that uses 
> subcommands (so that I can set up the documentation right)?

Here is a simple example that uses two ggo files. This "package"
consists of four files:

        subcmd.c: the main program
        first_cmd.ggo: command line options for the first subcommand
        second_cmd.ggo: command line options for the second subcommand
        Makefile

As the tarball for these four files is only 1188 bytes, it is attached
as well.

Have fun
Andre

-------------------------------------------------- subcmd.c
/* demonstrates the usage of more than one gengetopt file */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "first_cmd.cmdline.h"
#include "second_cmd.cmdline.h"

static unsigned split_args(char *args, char ***argv_ptr, int delim)
{
        char *p = args;
        char **argv;
        ssize_t n = 0, i;

        while ((p = strchr(p, delim))) {
                p++;
                n++;
        }
        *argv_ptr = malloc((n + 3) * sizeof(char *));
        argv = *argv_ptr;
        i = 0;
        p = args;
        while (p) {
                argv[i] = p;
                p = strchr(p, delim);
                if (p) {
                        *p = '\0';
                        p++;
                        i++;
                }
        }
        argv[n + 1] = NULL;
        return n + 1;
}

static void handler_first_cmd(int argc, char **argv)
{
        struct first_cmd_args_info fcai;

        printf("handling first command\n");
        first_cmd_cmdline_parser(argc, argv, &fcai);
        printf("okay, a = %d, b = %d\n", fcai.opt_a_arg,
                fcai.opt_b_arg);
}

static void handler_second_cmd(int argc, char **argv)
{
        struct second_cmd_args_info scai;
        printf("handling second command\n");
        second_cmd_cmdline_parser(argc, argv, &scai);
        printf("okay, b = %d, c = %d\n", scai.opt_b_arg,
                scai.opt_c_arg);
}

int main(int argc, char **argv)
{
        char line[255];
        int cmd_argc;
        char **cmd_argv;

        while (fgets(line, 255, stdin)) {
                if (strlen(line))
                        line[strlen(line) - 1] = '\0';
                cmd_argc = split_args(line, &cmd_argv, ' ');
                printf("cmd_argc: %d, cmd_argv[0]: %s\n", cmd_argc, 
cmd_argv[0]);
                if (!strcmp(cmd_argv[0], "first_cmd")) {
                        handler_first_cmd(cmd_argc, cmd_argv);
                        continue;
                }
                if (!strcmp(cmd_argv[0], "second_cmd")) {
                        handler_second_cmd(cmd_argc, cmd_argv);
                        continue;
                }
                printf("invalid command\n");
        }
        return 0;
}
-------------------------------------------------- first_cmd.ggo
option "opt_a" a "first option for first subcommand" int typestr="number" 
default="42" no
option "opt_b" b "second option for first subcommand" int typestr="number" 
default="43" no
-------------------------------------------------- second_cmd.ggo
option "opt_b" b "first option for second subcommand" int typestr="number" 
default="7" no
option "opt_c" c "second option for second subcommand" int typestr="number" 
default="8" no
-------------------------------------------------- Makefile
%_cmd.cmdline.h %_cmd.cmdline.c: %_cmd.ggo
        gengetopt $(module_ggo_opts) \
                --set-version="0.0.1" \
                --set-package=$(subst .ggo,,$<) \
                --file-name=$(subst .ggo,,$<).cmdline \
                --func-name $(subst .ggo,,$<)_cmdline_parser \
                --arg-struct-name=$(subst .ggo,_args_info,$<) < $<
all: subcmd

clean:
        rm -f first_cmd.cmdline.* second_cmd.cmdline.* *.o subcmd

subcmd.o: subcmd.c first_cmd.cmdline.h second_cmd.cmdline.h

%.o: %c
        $(CC) -c -o $@ $<

subcmd: subcmd.o first_cmd.cmdline.o second_cmd.cmdline.o
        $(CC) -o $@ subcmd.o first_cmd.cmdline.o second_cmd.cmdline.o
-- 
The only person who always got his work done by Friday was Robinson Crusoe

Attachment: gengetopt-subcommand.tar.gz
Description: Binary data


reply via email to

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