[program_options] Obtaining parameter order
Hi everyone, I'm writing a program that needs to know the order that parameters are supplied in, but I can't find anything in the docs about this. For example, a program to work with .zip files might work like this: zip --rename=one.bin:alpha.bin --add=one.bin --rename=one.bin:beta.bin The initial --rename would need to be acted upon first, before --add (otherwise one.bin would get overwritten) and likewise the last --rename must be acted upon after --add, otherwise the wrong file (or no file) would be renamed. If I use normal string vectors the order of the parameters will be lost so this won't work. Is this sort of parsing possible with program_options, or will I need to resort to writing my own command-line parsing code? Thanks, Adam.
Adam Nielsen wrote:
Hi everyone,
I'm writing a program that needs to know the order that parameters are supplied in, but I can't find anything in the docs about this.
For example, a program to work with .zip files might work like this:
zip --rename=one.bin:alpha.bin --add=one.bin --rename=one.bin:beta.bin
The initial --rename would need to be acted upon first, before --add (otherwise one.bin would get overwritten) and likewise the last --rename must be acted upon after --add, otherwise the wrong file (or no file) would be renamed. If I use normal string vectors the order of the parameters will be lost so this won't work.
Is this sort of parsing possible with program_options, or will I need to resort to writing my own command-line parsing code?
The parse_command_line function returns vector<options> which is accurate representation of what was found on the command line -- in particular: - the order of options is the same as on command line - if there are multiple occurrences of the same option, those are not collapsed and stay separate. Does this help? - Volodya
For example, a program to work with .zip files might work like this:
zip --rename=one.bin:alpha.bin --add=one.bin --rename=one.bin:beta.bin
The parse_command_line function returns vector<options> which is accurate representation of what was found on the command line -- in particular:
- the order of options is the same as on command line - if there are multiple occurrences of the same option, those are not collapsed and stay separate.
Does this help?
Yes! This works exactly as I need. Thanks! One question - parse_command_line() returns (eventually) a vector of basic_option. This basic_option class contains the name of the option and a vector of values. What is this vector used for? If a command line option has no value (e.g. "--verbose") then the vector is of course empty, and if the command line option has a value (such as "--output file.bin") then the vector contains a single value, so I'm wondering whether there's some syntax that will cause multiple values to end up in this vector, or is it there for use later on (when options are combined)? At any rate this has solved my problem so thanks for the pointer! Cheers, Adam.
Adam Nielsen wrote:
For example, a program to work with .zip files might work like this:
zip --rename=one.bin:alpha.bin --add=one.bin --rename=one.bin:beta.bin
The parse_command_line function returns vector<options> which is accurate representation of what was found on the command line -- in particular:
- the order of options is the same as on command line - if there are multiple occurrences of the same option, those are not collapsed and stay separate.
Does this help?
Yes! This works exactly as I need. Thanks!
One question - parse_command_line() returns (eventually) a vector of basic_option. This basic_option class contains the name of the option and a vector of values.
What is this vector used for? If a command line option has no value (e.g. "--verbose") then the vector is of course empty, and if the command line option has a value (such as "--output file.bin") then the vector contains a single value, so I'm wondering whether there's some syntax that will cause multiple values to end up in this vector, or is it there for use later on (when options are combined)?
You can declare an option that takes multiple values, so --such-an-option a b c --another-option will result in a vector of values containing 'a', 'b', and 'c'. - Volodya
participants (2)
-
Adam Nielsen
-
Vladimir Prus