nategood / commando

An Elegant CLI Library for PHP
MIT License
798 stars 80 forks source link

Multiple valued option - how to? #92

Closed OnkelTem closed 5 years ago

OnkelTem commented 6 years ago

I want to declare an option which could be invoked several times, e.g.:

--option value1 --option value2 --option value3

This is similar to what tar have for files exclusion, .e.g

$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .

I cannot find a way how to do this, or I'm missing something?

NeoVance commented 6 years ago

I don't think there is an option type for that specific case. You can use the count option type multiple times, but it does not take a value.

You can use the map or cast option to achieve that though. Just pass in a function that will add the value to an array, and return the array of values.

OnkelTem commented 6 years ago

You can use the map or cast option to achieve that though. Just pass in a function that will add the value to an array, and return the array of values.

Could you please provide an example? Because I tried map (not cast though) and it was called only once...

NeoVance commented 6 years ago

Wow. You are right. I totally missed that. I assumed it should work, but the tokens get deduped by array keys. Looks like it should be a fairly easy thing to fix though. I will look into putting up a PR soon.

NeoVance commented 6 years ago

See PR #93 or my branch accumulator-for-multiple-options-of-same-name 47135e1489a0b765f8269f17599c9bed117dcd4a

OnkelTem commented 6 years ago

@NeoVance thanks man! I'll try it and write back

NeoVance commented 6 years ago

Oh forgot to include the info.

reduce (Closure $reducer)

Aliases: list, each, every

Execute an accumulator/reducer function on every instance of the option in the command. Takes an accumulator function, and returns mixed (you can return any value). If you also supply a map for the option the map will execute on every value before it is passed to the accumulator function.

Signature: function(mixed $accumulated, mixed $value) : mixed

nategood commented 5 years ago

Merged 93