Re: Command line help options
- Posted by ChrisB (moderator) Nov 29, 2010
- 1525 views
Hi
Just done a little with the example, and if you copy and paste, it doesn't work, as is. May I submit
include std/cmdline.e include std/map.e include std/filesys.e sequence option_definition integer gVerbose = 0 sequence gOutFile = {} sequence gInFile = {} function opt_verbose( sequence value) if value[OPT_VAL] = -1 then -- (-!v used on command line) gVerbose = 0 else if value[OPT_CNT] = 1 then gVerbose = 1 else gVerbose += 1 end if end if return 1 end function function opt_output_filename( sequence value) gOutFile = value[OPT_VAL] return 1 end function function opt_extras( sequence value) if not file_exists(value[OPT_VAL]) then show_help(option_definition, sprintf("Cannot find '%s'", {value[OPT_VAL]}) ) abort(1) end if gInFile = append(gInFile, value[OPT_VAL]) return 1 end function option_definition = { { "m", "verbose", "Verbose output", { NO_PARAMETER }, routine_id("opt_verbose") }, --can't have duplicate options { "j", "hash", "Calc hash values", { NO_PARAMETER }, -1 }, --change this to h when hardcoded h fixed { "o", "output", "Output filename", { MANDATORY, HAS_PARAMETER, ONCE } , routine_id("opt_output_filename") }, { "i", "import", "An import path", { HAS_PARAMETER, MULTIPLE}, -1 }, { "v", "version", "Display version", { VERSIONING, "myprog v1.0" } }, { 0, 0, 0, 0, routine_id("opt_extras")} } map:map opts = cmd_parse(option_definition) -- When run as: -- eui myprog.ex -v @output.txt -i /etc/app input1.txt input2.txt -- and the file "output.txt" contains the two lines ... -- --output=john.txt -- '-i /usr/local' -- -- map:get(opts, "verbose") --> 1 -- map:get(opts, "hash") --> 0 (not supplied on command line) -- map:get(opts, "output") --> "john.txt" -- map:get(opts, "import") --> {"/usr/local", "/etc/app"} -- map:get(opts, OPT_EXTRAS) --> {"input1.txt", "input2.txt"}
Looking through cmdline, PAUSE_MSG doesn't seem to do anything when I try it.
Perhaps all the examples could be checked (not just by the devs, but by everyone) for copy and paste functionality.
Chris