Array arguments
liscio opened this issue · 7 comments
I've been using Console for building some internal tools, and it's been going great so far. Thanks for separating it from vapor, and for the recent ConsoleKit changes!
Anyway, I was wondering today whether you folks considered allowing for an argument to be specified multiple times? For instance, say I have an input
argument that can be specified multiple times. It'd be used like so:
my-command --input /path/to/input --input /path/to/other/input --output /path/to/output
Ideally, this would allow me to get an array of paths for the key "input"
from a CommandContext
.
Thoughts?
I like this. I'm wondering if maybe using a comma separated list might be better though?
my-command --input /path/to/input,/path/to/other/input --output /path/to/output
Is there a precedent for that usage? I can't think of one myself—especially one that plays nicely when used in bash
scripts, etc.
For instance, xcodebuild
uses the pattern I proposed to specify multiple localization languages (-exportLanguage
), find
allows you to specify many duplicated switches on the command line (like -or
predicates), and it's also common to think of input files in this way. Think, cat fileA fileB fileC > bigFile
, for instance.
I think it may be more common in the Python world? I haven't looked a whole lot. I'm fine going either way.
Just to chime in, Python's Click library does it the way @liscio mentioned but they also have an optional guard in the form of nargs
: https://click.palletsprojects.com/en/7.x/options/#multi-value-options
This seems like a worthwhile addition. I think we'd need a new property wrapper for it. Something like:
@Array(name: "input")
var inputs: [String]
@Array
name should be bike-shedded as there's probably something better. Maybe @Options
(with an s)?
You could also conform Array
to LosslessStringConvertible
.