Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Can you implement subcomands using with this library? #34

Open
jr-tutor opened this issue Sep 7, 2020 · 8 comments
Open

Question: Can you implement subcomands using with this library? #34

jr-tutor opened this issue Sep 7, 2020 · 8 comments

Comments

@jr-tutor
Copy link

jr-tutor commented Sep 7, 2020

Hi,

does your library support subcomands such as used in GIT or Argparse?

e.g.

git add *
git --help clone
etc.

Thank you

@cofyc
Copy link
Owner

cofyc commented Sep 7, 2020

no builtin support, but it's easy to implement, e.g.

struct cmd_struct {
    const char *cmd;
    int len;
    int (*fn) (int, const char **);
    const char *help;
};

static struct cmd_struct commands[] = { 
    {"sub", 3, sub_cmd, NULL},
    ...
}

argc = argparse_parse(&argparse, argc, argv);
if (argc < 1) {
    // show help
}   

/* Try to run command with args provided. */
struct cmd_struct *cmd = NULL;
for (int i = 0; i < ARRAY_SIZE(commands); i++) {
    if (!strcmp(commands[i].cmd, argv[0])) {
        cmd = &commands[i];
    }   
}  

// run the command

@a-random-lemurian
Copy link
Contributor

a-random-lemurian commented Feb 9, 2022

Are there plans to add subcommand support to the library?

P.S. Oh, there was a closed issue about subcommands.

@cofyc
Copy link
Owner

cofyc commented Feb 10, 2022

no, however you can follow this example

@a-random-lemurian
Copy link
Contributor

a-random-lemurian commented Feb 10, 2022

no, however you can follow this example

that example just causes a segfault when I cmd->fn(argc, argv), what should I put in place of // run the command?

P.S I finally got a working example.... I'm gonna push it real quick
P.S[2] Here's the working example https://github.com/a-random-lemurian/uselinux/blob/main/src/rmbloat/rmbloat.c

@cofyc
Copy link
Owner

cofyc commented Feb 10, 2022

I also create a full example here: https://github.com/cofyc/argparse/blob/master/tests/subcommands.c

@a-random-lemurian
Copy link
Contributor

I also create a full example here: https://github.com/cofyc/argparse/blob/master/tests/subcommands.c

LGTM, but unfortunately it doesn't say anything about adding options to the subcommands themselves.

@cofyc
Copy link
Owner

cofyc commented Feb 11, 2022

If you need to parse arguments for subcommand, you can init and config argparse to parse arguments in the same way as in the main program.

@cofyc
Copy link
Owner

cofyc commented Feb 11, 2022

the example is updated, see the latest version.

Note that ARGPARSE_STOP_AT_NON_OPTION must be passed to argparse_init in the main program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants