1.0.93 • Published 4 years ago

sg-clihelp v1.0.93

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

Cli Help

Utilities to help write command-line apps.

Usage

npm install sg-clihelp

Then, writing a short script or command-line app is easy.

The copy-and-paste version is below.

const sg          = require('sg-clihelp');      // Just for explanation, use require('sh-clihelp').all(), as below.
const {os,path}   = sg;
const {sh}        = sg;                         // https://github.com/shelljs/shelljs
const {test}      = sg.sh;
const {execa}     = sg;                         // https://github.com/sindresorhus/execa
const ARGV        = sg.ARGV();
const ENV         = sg.ENV();

// Do not be too eager if we are just being required
if (require.main === module) {
  sg.runTopAsync(main);
}

async function main() {
  const foo   = ARGV.foo;
  const bar   = ENV.at('BAR');

  const confDir = path.join(os.homedir(), 'quxxdir');

  if (!test('-d', confDir)) {
    return sg.dieAsync(`Need ${confDir}`);
  }

  const configFile = path.join(confDir, 'config.json');
  if (!test('-f', configFile)) {
    return sg.dieAsync(`Need ${configFile}`);
  }

  const battConfig = sg.from(confDir, 'config.json', 'foo.bar.batt');

  // ...

  const cmdStdout = await execa.stdout(sh.which('command').toString(), ['arg1', 'arg2']);
  console.log(sg.splitLn(cmdStdout));

  // ...
}

Copy-and-Paste

This is the version to start with. Uncomment as needed.

const {sg,fs,path,os,util,sh,die,dieAsync,grepLines,include,from,startupDone,runTopAsync,exec,execa,execz,exec_ez,find,grep,ls,mkdir,SgDir,test,tempdir,inspect,glob} = require('sg-clihelp').all();

//const tmp         = require('tmp');             // https://github.com/raszi/node-tmp
//const crypto      = require('crypto');

//require('loud-rejection/register');             // https://github.com/sindresorhus/loud-rejection
//require('hard-rejection/register');             // https://github.com/sindresorhus/hard-rejection
//require('exit-on-epipe');                       // https://github.com/SheetJS/node-exit-on-epipe

const ARGV        = sg.ARGV();
const ENV         = sg.ENV();

// Make sure we are being invoked, not just being `required()`.
if (require.main === module) {
  sg.runTopAsync(main);
}



//-----------------------------------------------------------------------------------------------------------------------------
async function main() {
  const foo   = ARGV.foo;
  const bar   = ENV.at('BAR');

  const confDir = path.join(os.homedir(), 'quxxdir');

  if (!test('-d', confDir)) {
    return sg.dieAsync(`Need ${confDir}`);
  }

  const configFile = path.join(confDir, 'config.json');
  if (!test('-f', configFile)) {
    return sg.dieAsync(`Need ${configFile}`);
  }

  const battConfig = sg.from(confDir, 'config.json', 'foo.bar.batt');

  // ...

  const cmdStdout = await execa.stdout(sh.which('command').toString(), ['arg1', 'arg2']);
  console.log(sg.splitLn(cmdStdout));

  // ...
}

Included

Included with sg-clihelp:

  • sg.die(msg, code =113) - shows msg, then process.exit(code)
  • sg.dieAsync(msg, code =113) - shows msg, then process.exit(code) - Use with runTopAsync.
  • sg.grepLines(regex, filename) - sends lines that match regex from filename to stdout
  • sg.include(dirname, filename) - Safe include.
  • sg.from(dirname, filename, key) - Get value from JSON file.
  • sg.runTopAsync(main, name='main') - If main is a top-level async function, use this to run it.
  • sg.startupDone(ARGV, modfilename, failed, msg) - messaging for --help and failed

From sg0:

  • sg.safeJSONParse(json)
  • sg.safeJSONStringify(obj)
  • sg.splitLn(str)
  • sg.deref(obj, keys)
  • sg.setOn(obj, keys, value)
  • sg.setOna(obj, keys, value)

Other modules bundled:

  • sg0
  • sg-argv
    • sg.ARGV()
  • sg-env
    • sg.ENV()
  • sg-exec
    • execa
    • exec from shelljs
    • exec_ez from sg-exec
  • sg.fs - Node.js fs module.
  • sg.path - Node.js path module.
  • sg.os - Node.js os module.
  • sg.util - Node.js util module.
  • sg.sh - shell.js module.
1.0.93

4 years ago

1.0.92

4 years ago

1.0.88

4 years ago

1.0.90

4 years ago

1.0.87

4 years ago

1.0.86

4 years ago

1.0.85

4 years ago

1.0.84

4 years ago

1.0.83

4 years ago

1.0.82

4 years ago

1.0.80

4 years ago

1.0.81

4 years ago

1.0.79

4 years ago

1.0.78

4 years ago

1.0.77

4 years ago

1.0.76

4 years ago

1.0.75

4 years ago

1.0.74

4 years ago

1.0.73

4 years ago

1.0.72

4 years ago

1.0.69

4 years ago

1.0.68

4 years ago

1.0.67

4 years ago

1.0.66

4 years ago

1.0.65

4 years ago

1.0.64

5 years ago

1.0.63

5 years ago

1.0.62

5 years ago

1.0.61

5 years ago

1.0.60

5 years ago

1.0.59

5 years ago

1.0.58

5 years ago

1.0.57

5 years ago

1.0.56

5 years ago

1.0.55

5 years ago

1.0.54

5 years ago

1.0.53

5 years ago

1.0.52

5 years ago

1.0.51

5 years ago

1.0.50

5 years ago

1.0.49

5 years ago

1.0.48

5 years ago

1.0.47

5 years ago

1.0.46

5 years ago

1.0.45

5 years ago

1.0.44

5 years ago

1.0.43

5 years ago

1.0.42

5 years ago

1.0.41

5 years ago

1.0.40

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.31

5 years ago

1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago