2.1.0 • Published 3 years ago

flarser v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Flarser

Command-line flag parser.

license

Example usage:

// import { Flarser, Flag } from 'flarser'
// import type { FlarserError } from 'flarser'
const { Flarser, Flag } = require('flarser')

function someFunction() {

  try {

    const flarser = new Flarser(
      new Flag('help')                                         // --help                     Required
        .setAlias('h')                                         // -h                         Optional
        .setDescription('Usage: --help <command>')             // Description                Optional
    )
      .flagPrefix('--')                                        // Flag prefix                Default: '--'
      .flagAliasPrefix('-')                                    // Flag alias prefix          Default: '-'
      .caseSensitive(true)                                     // Case sensitive             Default: true
      .ignoreUnknownFlags(true)                                // Ignore unregistered flags  Default: false
      .noValues()                                              // If called, this flag will not accept any value
      // parse should always at the end of the chain.
      .parse(process.argv.slice(2).join(' '))                  // Parse command-line input

    /**
     * flarser.getFirstEntries(): Array<string>                // First entries that are not flags.
     * flarser.getFlags(): Array<Flag>                         // Returns list of all registered flags.
     * flarser.getFlagByName(name: string): Flag | null        // Returns a single flag instance (using its name) that is registered.
     * flarser.getFlagByAlias(name: string): Flag | null       // Returns a single flag instance (using its alias) that is registered.
     */

    // Get reference of `help` flag
    const helpFlag = flarser.getFlagByName('help')

    /**
     * helpFlag.getName(): string                              // Returns the name of the flag.
     * helpFlag.getAlias(): string | null                      // Returns the alias of the flag.
     * helpFlag.getDescription(): string                       // Returns the description of the flag.
     * helpFlag.getValues(includeUnknownFlags): Array<string>  // Returns the values of the flag.
     * helpFlag.getStatus(key?: string): any                   // Get status object or its value.
     */

    // If help flag is used and it has no value
    if (helpFlag.getStatus('iscalled') && helpFlag.getValues().length < 1) {
      return console.log(helpFlag.getDescription())
    }

    /* Do more stuffs */

    /* } catch (error: FlarserError) { */
  } catch (error) {
    // handle error.
    console.log('Error:', error.message)
  }

}

someFunction()
1.0.6

3 years ago

1.0.5

3 years ago

2.1.0

3 years ago

1.0.4

3 years ago

2.0.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago