git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC] git-fsck-cache argument processing

From
JGJeff Garzik <jgarzik@pobox.com>
Date
May 21, 2005, 04:08 UTC
Message-ID
<428EB444.7010200@pobox.com>
In-Reply-To
<4870.10.10.10.24.1116646732.squirrel@linux1>
Sean wrote:
Show 8 quoted lines
> Here is a first crack at using argp as suggested by Jeff Garzik to
> implement argument processing as requested by Junio and Linus.  Each of
> the long arguments have been given a single character equivalent as well.
> 
> This patch only converts fsck-cache to use argp in case anyone has
> objections to the basic format or style.  The patch includes a version
> number inside of fsck-cache.c; this should really be in a separate include
> file so you can run any command with --version and get the same answer.
Pretty good.  You'll probably want some additional changes:
1) eliminate
+       case ARGP_KEY_ARG:      state->next = state->argc; break;
This will cause option processing to stop at the first unknown argument.
2) Pass-by-reference a variable to argp_parse(), which will store the 
index of the argument where processing stopped.  This is the first 
hash/file/etc. non-option argument.
(example code from posixutils)
int parse_cmdline(struct cmdline_walker *cw)
{
       error_t rc_argp;
       int idx = 0;
       rc_argp = argp_parse(cw->argp, cw->argc, cw->argv, 0, &idx, NULL);
       if (rc_argp) {
               fprintf(stderr, "argp_parse: %s\n", strerror(rc_argp));
               return -rc_argp;
       }
       return idx;
}

'idx' in this case is the first non-option argument, which can be passed directly to argv[]. From there, you perform standard iteration over the arguments provided on the command line, starting at argv[idx].

If you have a fixed number of arguments following the options, then your parse_opt function can easily parse those args as well:

static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
         switch (key) {
         case '1':
                 outmask |= OPT_FILE1;
                 break;
         case '2':
                 outmask |= OPT_FILE2;
                 break;
         case '3':
                 outmask |= OPT_DUP;
                 break;
         case ARGP_KEY_ARG:
           switch(state->arg_num) {
           case 0:         file1 = arg; break; /* 1st non-opt arg */
           case 1:         file2 = arg; break; /* 2nd non-opt arg */
           default:        argp_usage (state); break; /* too many args */
           }
           break;
         case ARGP_KEY_END:
                 if (state->arg_num < 2)         /* not enough args */
                         argp_usage (state);
                 break;
         default:
                 return ARGP_ERR_UNKNOWN;
         }
         return 0;
}
Previous: SeanNext: Sean
Message 2 of 18 in “[RFC] git-fsck-cache argument processing”
  1. SeanMay 21, 2005
  2. Jeff GarzikMay 21, 2005
  3. SeanMay 21, 2005
  4. Jeff GarzikMay 21, 2005
  5. Junio C HamanoMay 21, 2005
  6. Jeff GarzikMay 21, 2005
  7. Junio C HamanoMay 21, 2005
  8. Olivier GalibertMay 21, 2005
  9. Jeff GarzikMay 21, 2005
  10. SeanMay 21, 2005
  11. Olivier GalibertMay 21, 2005
  12. SeanMay 21, 2005
  13. Jeff GarzikMay 21, 2005
  14. Joel BeckerMay 21, 2005
  15. Linus TorvaldsMay 21, 2005
  16. SeanMay 21, 2005
  17. SeanMay 21, 2005
  18. Linus TorvaldsMay 21, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.