-
Notifications
You must be signed in to change notification settings - Fork 23
feat: allow multiple for arg #63
base: main
Are you sure you want to change the base?
Conversation
Thanks for the contribution! Before we can merge this, we need @mkg20001 to sign the Salesforce.com Contributor License Agreement. |
NOTE: I have no clue about typescript. I'd appreciate some guiadance |
Codecov Report
@@ Coverage Diff @@
## master #63 +/- ##
==========================================
- Coverage 84.45% 0.00% -84.46%
==========================================
Files 11 1 -10
Lines 328 29 -299
Branches 91 14 -77
==========================================
- Hits 277 0 -277
+ Misses 36 29 -7
+ Partials 15 0 -15
Continue to review full report at Codecov.
|
@RasPhilCo Any chance to get this merged? |
I think we should fix this by fixing how parse handles multiple args with static = false set. It solves this without adding api. See oclif/oclif#234 (comment) |
@RasPhilCo This extra API would be extremly convinient for some usecases. Like parsing "<src <src...>> <dst>" (like cp does it), automatically |
On further thought, I'm on board with this. I'll make a few typescript comments. It'll need tests. |
src/args.ts
Outdated
@@ -22,11 +22,13 @@ export interface ArgBase<T> { | |||
|
|||
export type RequiredArg<T> = ArgBase<T> & { | |||
required: true | |||
multiple: boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
multiple?: false
src/args.ts
Outdated
value: T | ||
} | ||
|
||
export type OptionalArg<T> = ArgBase<T> & { | ||
required: false | ||
multiple: boolean |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
multiple?: false
I'm in need of this feature too, @mkg20001 if you allow me I can try to contribute on this PR, at least trying to make some tests |
@tiagonapoli You've got push access now: https://github.com/mkg20001/parser/invitations |
@mkg20001 if you could rebase this, I'm happy to help write tests :) |
5454424
to
394002a
Compare
@G-Rath I've added you as a collaborator: https://github.com/mkg20001/parser/invitations |
394002a
to
e83641e
Compare
e83641e
to
d8f27c3
Compare
@mkg20001 I've had a play with this, but it doesn't seem to work anymore? Am I missing something:
|
Hrm. I changed |
Correct, that won't have changed the behaviour. Types exist at compile only - they never affect the emitted js (that's one of the non-goals of TS) :) |
@mkg20001 ah it's because now the I feel like this makes
|
@mkg20001 This doesn't seem to be working as expected still: it's not properly handling args after the multiple:
I just want to check if this is the intended behaviour? |
Looking further into this, I think the answer is what @RasPhilCo originally said:
Effectively by having This should provide a simple but powerful api for supporting variadic arguments: Commands that take at 1 or more args:
Commands that take multiple arguments before a final argument (a la
I believe the complexity to implement the proposed API above is much smaller than what it would take to implement this PR. |
Any chance of getting this implemented? Any workaround? Support for array-like arguments ( |
Hey maintainers, can I do anything to help push this forward? |
I'm no longer interested in the development of this PR. Feel free to take my changes and implement them in another PR |
I am interested in this feature and would be willing to put in some time to get it polished, as long as the maintainers can say that they're willing to merge it. I think this is a very handy feature for when one wants to have a command like I also think it is very consistent with oclif's current design, because flags already have a |
I found a workaround, in case anyone else finds this useful: static args = [{ name: 'path' }];
static strict = false // Allow variable length arguments (https://oclif.io/docs/args)
...
getAllPathArgsArray() {
const { args } = this.parse(TestCommand)
const indexOfFirstPath = this.argv.findIndex(arg => arg === args.path)
return this.argv.slice(indexOfFirstPath)
} or perhaps if you're already calling const oclifParseResult = this.parse(TestCommand)
const { flags, args } = oclifParseResult
const allPathArgsArray = this.getAllPathArgsArray(oclifParseResult)
getAllPathArgsArray(oclifParseResult) {
const indexOfFirstPath = this.argv.findIndex(arg => arg === oclifParseResult.args.path)
return this.argv.slice(indexOfFirstPath)
} I do find it surprising that oclif doesn't just support a |
Inspired by @msabramo I was able to work around this by using the
Results in a parse result like this:
Which I then filtered the
|
This enables the multiple flag for an argument
The advantage over #61 is that this allows "multiple" arguments right in the middle, although both proposals could co-exist aswell
NOTE: Only one multiple argument is allowed, otherwise they will eat each other's arguments
The strategy to parse it works by checking how many non-multiple arguments should follow