decarg
TypeScript icon, indicating that this package has built-in type declarations

1.3.0 • Public • Published

decarg

decorator based cli arguments parser

npm i decarg pnpm add decarg yarn add decarg

Examples

# basic
    # view source example/basic.ts

    import { arg, decarg } from 'decarg'
    
    class Options {
      @arg('<file> [<file>, decarg.]', 'Files to process') file!: string[]
    
      @arg('--', '[decarg.rest]', 'The rest of the arguments') passArgs = []
    
      @arg('-f', '--flag', 'Flag about something') flag = false
    
      @arg('-c', '--count', 'How many times') count = 42
    
      @arg('-C', 'Choose color', ['blue', 'red', 'yellow']) color = 'blue'
    
      @arg('-s', '--string', 'Some string') string = 'hmm'
    
      @arg('--meh', 'Meh') meh: string[] = []
    
      static examples = {
        '-f foo': 'Convert foo by force',
        '-f foo -s': 'Convert foo by force smoothly',
      }
    }
    
    const options = decarg(new Options())
    
    // these fail:
    
    // const options = decarg(new Options(), ['exec'])
    // const options = decarg(new Options(), ['exec', '-C'])
    // const options = decarg(new Options(), ['exec', '-C=green'])
    // const options = decarg(new Options(), ['exec', '-c=not a number'])
    
    // this passes:
    
    // const options = decarg(new Options(), ['exec', 'foo'])
    
    console.log(options)

API

# OptionExpectedValueError src/errors.ts#L20

# stackTraceLimit

    number

# captureStackTrace(targetObject, constructorOpt)

    # targetObject

      object

    # constructorOpt

      Function

    captureStackTrace(targetObject, constructorOpt)  =>

      void
# OptionHelpRequested src/errors.ts#L49
# stackTraceLimit

    number

# captureStackTrace(targetObject, constructorOpt)

    # targetObject

      object

    # constructorOpt

      Function

    captureStackTrace(targetObject, constructorOpt)  =>

      void
# OptionInvalidValueError src/errors.ts#L35
# stackTraceLimit

    number

# captureStackTrace(targetObject, constructorOpt)

    # targetObject

      object

    # constructorOpt

      Function

    captureStackTrace(targetObject, constructorOpt)  =>

      void
# OptionValidationError src/errors.ts#L8
# stackTraceLimit

    number

# captureStackTrace(targetObject, constructorOpt)

    # targetObject

      object

    # constructorOpt

      Function

    captureStackTrace(targetObject, constructorOpt)  =>

      void
# arg(description) src/arg.ts#L7

    # description

      ArgDescription

    arg(description)  =>

      {

      # target

        Function

      (target)  =>

        void
      # target

        Object

      # propertyKey

        string | symbol

      (target, propertyKey)  =>

        void
      }
# decarg(target, argv, { exit, log }) src/index.ts#L17

    # target
    # argv  =  ...

      string []

    # overrides  =  {}

      {

      # exit

        undefined |

        # (code)

          # code

            number

          (code)  =>

            never

# log

    undefined | {

    # data

      any []

    (data)  =>

      void
    # data

      any []

    (data)  =>

      void
    # message

      any

    # optionalParams

      any []

    (message, optionalParams)  =>

      void
    }
}

decarg<T>(target, argv, { exit, log })  =>

    undefined | T
# parse(target, argv) src/parse.ts#L17

    # target
    # argv  =  ...

      string []

    parse<T>(target, argv)  =>

Credits

Contributing

Fork or edit and submit a PR.

All contributions are welcome!

License

MIT © 2022 stagas

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.3.083latest

Version History

VersionDownloads (Last 7 Days)Published
1.3.083
1.2.110
1.2.00
1.1.00
1.0.20
1.0.11
1.0.00

Package Sidebar

Install

npm i decarg

Weekly Downloads

94

Version

1.3.0

License

MIT

Unpacked Size

371 kB

Total Files

38

Last publish

Collaborators

  • stagas