parsingfilenamescmdline-args

How should an application parse a list of filenames from cmdline args?


I'd like to write an app, my_app, that takes a list of named options and a list of filenames from the cmdline, e.g.,

% my_app --arg_1 arg_1_value filename_1 filename_2

The filenames are the last args and are not associated with any named options.

From the cmdline parsers, e.g., flag in Golang, that I've worked, it seems that the parsers will only extract the args that are configured, and that I'd need to identify the list of filenames manually by walking thru the original argv[] list.

I'd like to ask if there are parsers (or their options that I may have overlooked) that can also extract those filenames, or they only return the unprocessed args, and therefore, I could assume that these are the filenames.


Solution

  • The Golang flag module makes the trailing arguments available as the slice flag.Args, which is the trailing part of os.Args.

    That's a pretty typical way for command-line argument parsers to work, although the details will vary according to language. The standard C library argument parser, fir example, provides the global optind, which is the index in argv of the first non-flag argument.