node-dashdash icon indicating copy to clipboard operation
node-dashdash copied to clipboard

methodology for creating top level actions à la Dockêr

Open ORESoftware opened this issue 8 years ago • 2 comments

With docker in mind, we have a command line API like so:

docker run
docker build
docker ps

or with Istanbul in mind:

istanbul cover
istanbul report
istanbul instrument

or git:

git merge
git rebase
git checkout

etc.

is there a methodology for creating this kind of top-level action api with dashdash?

AFAICT, you'd have to make a homegrown system from dashdash, and from what I can tell, you'd have to enforce that the first argument after docker was the action.

Right now, I have a CLI API using dashdash, but it's more akin to:

docker --run
docker --cp
docker --ps
docker --create
docker --images

where the actions are all just booleans according my dashdash config.

Just something to discuss.

I think it would be a neat feature if dashdash could take an initialization argument to interpret the first x _args as actions. And these would be stored as a separate property on opts.

So it would be something like this:

const dashdash = require('dashdash');

let opts, parser = dashdash.createParser({options: options}, {
   actions: 1  // => the first arg is interpreted as an action, and it has to appear before any options 
});

try {
  opts = parser.parse(process.argv);
} catch (err) {
 // 
}

// opts => {};
// opts._args => []
// opts._actions => [] 

ORESoftware avatar Jul 01 '17 00:07 ORESoftware

We can easily use a shell script to do this:

first_arg="$1";
shift 1;

if [ "$first_arg" == "merge" ]; then

  "$root/merge" "$@"

elif [  "$first_arg" == "rebase"  ]; then

  "$root/rebase" "$@"

elif [  "$first_arg" == "checkout"  ]; then

  "$root/checkout" "$@"

else 
   echo "whatevs"
fi

I have a nice way of using different dashdash options for each action/subcommand, which is probably the right way to do it.

my only remaining question/problem is how to get bash completion for git/docker style actions/subcommands

@trentm do you know how to get bash completion using dashdash, the way you can get bash completion for git subcommands? when I type in git me and then hit tab, I get git merge, that's what I mean. With dashdash, I can only get completion for something like git --merge.

ORESoftware avatar Sep 05 '18 06:09 ORESoftware

@ORESoftware did you check what https://github.com/trentm/node-cmdln offers?

On the first glance it sounds like what you are looking for.

karfau avatar Jan 31 '20 16:01 karfau