DEV Community

Cover image for Split commands and options from CLI
Lucas.T
Lucas.T

Posted on

Split commands and options from CLI

Hi 👋

This is a very small 1.33 KB package that does just one simple task to get the user's input data passed from your terminal, split it and send it back to and object, That's it, no big config or API just a tiny function.

Install

npm i argv-user-input
Enter fullscreen mode Exit fullscreen mode

Usage 💡

#!/usr/bin/env node
import parseArgvData from 'argv-user-input';
const argvs = parseArgvData();
Enter fullscreen mode Exit fullscreen mode

foo.js

With no commands or options.

$ foo.js 
Enter fullscreen mode Exit fullscreen mode
console.log(argvs);
/*
{
  commands: [],
  options: {},
}
*/
Enter fullscreen mode Exit fullscreen mode

With commands and no option.

$ foo.js start test
Enter fullscreen mode Exit fullscreen mode
console.log(argvs);
/*
{
  commands: ['start', 'test'],
  options: {},
}
*/
Enter fullscreen mode Exit fullscreen mode

With commands and options.

$ foo.js start test --skip -p ./dev
Enter fullscreen mode Exit fullscreen mode
console.log(argvs);
/*
{
  commands: ['start', 'test'],
  options: {
    skip: true,
    p: './dev'
  },
}
*/
Enter fullscreen mode Exit fullscreen mode

With option and no command.

$ foo.js --name=foo
Enter fullscreen mode Exit fullscreen mode
console.log(argvs);
/*
{
  commands: [],
  options: {
    name: 'foo'
  },
}
*/
Enter fullscreen mode Exit fullscreen mode

Good code and have fun ✨

Top comments (0)