1.2.7 • Published 5 years ago

easy-options v1.2.7

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

version size downloads issues license

easy-options

npm i -D easy-options

use-image

Description これは何?

This is a module that makes it easy to create options.

Automatically detect and add short handlers and types.

You can also set a configuration file.

これは簡単にオプションを作成できるモジュールです。

ショートハンドラや型等を自動判別して追加してくれます。

また。コンフィグファイルも設定することが可能です。

View on npm

View on GitHub

特徴

  • Automatically add short handlers.

  • ショートハンドラを自動追加します。

  • Automatic type detection.

  • 型を自動判別します。

  • Handle misconfigurations.

  • 設定ミスをハンドリングします。

  • The setting list can be managed individually.

  • 設定リストを分けて管理することが可能です。

const EasyOptions = require('easy-options')

const easyOption = new EasyOption()
  .set('foo', {value: 'foo'})

const easyOptions2 = new EasyOption()
  .set('bar', {value: 'bar'})


easyOption.get('foo') // 'foo'
easyOptions2.get('foo') // undefined

easyOptions.keys() // ['foo']
easyOptions2.keys() // ['bar']

コマンドライン

command [...entry] [-key|--key] [value]
  • entry

    Stored in the entry value declared as an option.

    オプションで宣言されているentryの値に格納されます。

  • key

    Optional name. Declare a short handler with -key and a name with -key.

    オプションの名前です。-keyでショートハンドラ、--keyで名前を宣言します。

  • value

    The value to add to the option.

    The option to save is the previous key.

    オプションに追加する値です。

    格納するオプションは一つ前のkeyになります。

リファレンス

const EasyOptions = require('easy-options')
const easyOptions = new EasyOptions(name)
  • name: string?

    Command line command name to collect. A command line option with this name is ignored.

    This argument is not declared. If not declared, collect all command line options.

    収集するコマンドラインのコマンド名です。この名前のコマンドラインオプション以外は無視されます。

    この引数は宣言しなことも可能です。宣言しなかった場合は全てのコマンドラインオプションを収集します。

set

easyOptions.set(name, options)

オプションを定義します。

  • name: string

    Define the option name. The call method from the command line is --(name)

    オプション名を定義します。コマンドラインから呼ばれる場合は--(name)となります。

  • options: Object | string

    Details of the option. The string is treated the same as {value: false, description: (options)}.

    オプションの詳細です。文字列は{value: false, description: (options)}と同じ扱いになります。

    • value: any

      Optional initial value. Declaring true will switch when entered from the command line.

      オプションの初期値です。trueを宣言した場合、コマンドラインから入力された時点でトグルされます。

    • shor: string|boolean

      Short handler. If there is no duplication, the initial letter of the option name is set automatically. Declare false when not using a short handler.

      ショートハンドラです。重複しなければオプション名の頭文字を自動で設定します。ショートハンドラを使用しない場合はfalseを宣言します。

    • type: string

      The type of variable. If an initial value exists, it is automatically created with that type.

      変数のタイプです。初期値が存在している場合は、その型で自動作成します。

    • entry: number|boolean

      Get a variable with no arguments. If you declare true, you will get the variable set without options in the order in which they were set. If numbers are declared, use the variables in that order. The first value is 1.

      引数の無い変数を取得します。trueを宣言した場合オプションの無い変数setした順番に取得します。数字が宣言された場合はその順番の変数を使用します。先頭の値は1です。

    • description: string

      A description of the function. You can change the value from the command line only after declaring this variable.

      関数の説明です。この変数を宣言して初めてコマンドラインから値を変更できるようになります。

    このメソッドはメソッドチェーンで記述出来ます。

    easyOptions
      .set('foo', 'bar')
      .set('baz', 'qux')

file

easyOptions.file(file[, errorHandler])

Set the configuration file.

If declared more than once, the file declared later takes precedence.

コンフィグファイルを設定します。

複数回宣言された場合は後に宣言されたファイルが優先されて結合されます。

  • file: string

    The path of the file to be read.

    The root is the same as the app root (fs module root).

    読み込むファイルのパスです。

    ルートはアプリのルート(fsモジュールのルート)と同じです。

  • errorHandler: Function?

    Error handling when the file cannot be converted to settings.

    The content of error is given to the argument.

    This argument can be omitted.

    If omitted, the error is ignored.

    ファイルが設定に変換出来なかった場合のエラー処理です。

    引数にはerrorの内容が与えられます。

    この引数は省略する事が可能です。

    省略した場合はエラーを無視します。

This method can be described in a method chain.

このメソッドはメソッドチェーンで記述出来ます。

easyOptions
  .file('foo.config.json')
  .file('bar.config.json')
  .set('foo', 'bar')

get

const value = easyOptions.get(name)

Get the parameters.

パラメータを取得します。

Declare this method after the file and set methods.

このメソッドはfile, setメソッドの後に宣言してください。

  • value: any

    Optional return value.

    Values ​​are prioritized in the order of values ​​defined on the command line, config file, and set method.

    オプションの戻り値です。

    値はコマンドライン、コンフィグファイル、setメソッドで定義したvalueの順で優先されます。

  • name: string

    Option name to get.

    取得するオプション名です。

delete

easyOptions.delete(name)

Remove option.

オプションを削除します。

  • name: string

    Option name to delete.

    削除するオプション名です。

clear

easyOptions.clear()

Clear the parameters.

The command line for help and version is not deleted.

パラメータをリセットします。

help, version のコマンドラインは削除されません。

keys

const keys = easyOptions.keys()

Get a list of parameter names.

パラメータの名前のリストを取得します。

help and version are not included in the list.

help, versionはリストに含まれません。
  • keys: Array<string>

    The acquired option list.

    取得したオプションリストです。

list

const list = easyOptions.list()

Get a list of parameters in the form of {key: value}.

パラメータのリストを{ key: value }の形で取得します。

help and version are not included in the list.

help, versionはリストに含まれません。

type

easyOptions.type(name, callback[, onceFrag = true])

Define a new type for the option.

オプションに新しい型を定義します。

  • name: string

    The name of the type.

    型の名前です。

  • name: Function

    Variable to convert type. The value from the command line is inserted into the argument.

    Be sure to return the changed value with return.

    型を変換する変数です。引数にはコマンドラインからの値が挿入されます。

    必ず変更後の値をreturnで返してください。

  • onceFrag: boolean = true

    Whether to return an error when multiple arguments are declared. The initial value is true.

    複数の引数が宣言された時にエラーを返すかどうかです。初期値はtrueです。

usage

easyOptions.usage()

Displays command line usage on the console.

コマンドラインの使い方をコンソールに表示します。

help

easyOptions.help([...commands])

Displays a list of options available for the console.

コンソールに使用可能なオプションリストを表示します。

  • commands: ...string?

    The command name to be displayed.

    This argument can be omitted. If omitted, all options are displayed.

    表示するコマンド名です。

    この引数は省略できます。省略した場合は全てのオプションを表示します。

run

easyOptions.run()

Activate the command line.

コマンドラインを有効化します。

It is automatically enabled when you declare get, list, keys, usage and help methods.

Declare them only if you are not using those methods.

get, list, keys, usage, helpのメソッドを宣言した時に自動的に有効化されます。

それらのメソッドを使用しない場合にのみ宣言してください。

メソッド

color

console.log(`${easyOptions.color.red}sample${easyOptions.color.reset}`)

Change the text color when displayed on cli.

cliに表示する時の文字色を変更します。

  • reset: 配色をリセットします。
  • black: 黒色
  • red: 赤色
  • green: 緑色
  • yellow: 黄色
  • blue: 青色
  • magenta: 紅紫色
  • cyan: 水色
  • white: 白色

echo

easyOptions.echo.warn(message[, ...args])
// [!]Warn: message ...args
easyOptions.echo.error(message[, ...args])
// [!]Error: message ...args

Display a message on the command line.

error terminates processing.

コマンドラインにメッセージを表示します。

errorは処理を終了します。

  • message: string

    Message to display.

    表示するメッセージです。

  • args: ...any?

    Argument to display in the message. The text color is automatically changed according to the type of the argument.

    メッセージに表示する引数です。引数に型によって自動的に文字色を変更します。

Sample

Array type options.

Array タイプのオプション。

When entered from the command line etc., the element is inserted after emptying.

コマンドライン等から入力された時は要素を空にしてから挿入されます。
easyOptions
  .set('Array', {
    value: ['foo', 'bar'],
    description: 'Array type sample.'
  })
node sample --Array afterFoo afterBar
> Array ['afterFoo', 'afterBar']

Json type option.

Jsonタイプのオプションです。

easyOptions
  .set('json', {
    value: { foo: 'foo' },
    description: 'JSON type sample.'
  })
node sample --json afterFoo:foo afterBar:0
> jaons {afterFoo:'foo', afterBar:'0'}

Todo

  • 機械翻訳の修正。

Release Note

  • 2019/10/24 ver1.2.7

    • Bug fix when getting array type and Json type.
    • arrayタイプとJsonタイプを取得する際のバグ修正。
    • Changed the return value of the list method.
    • listメソッドの戻り値を変更。
    • Added English to readme by machine translation.
    • 機械翻訳でreadmeに英語を追加。
  • 2019/10/15 ver1.2.6

    • Fixed boolean type behavior. Changed to toggle without argument.
    • boolean型の挙動の修正。引数無しでトグルするように変更。
  • 2019/10/15 ver1.2.5

    • Fixed a bug that caused an error when the configuration file does not exist.
    • 設定ファイルが存在しない時にエラーになるバグの修正。
  • 2019/10/14 ver1.2.4

    • Sort help in alphabetical order.
    • ヘルプを五十音順に並び替え。
  • 2019/10/14 ver1.2.2

    • Fixed bug that initial issues warning.
    • initialが警告を出すバグの修正。
  • 2019/10/09 ver1.2.2

    • Fixed bug that initial issues warning.
    • initialが警告を出すバグの修正。
  • 2019/10/09 ver1.2.0

    • Changed option summing process.
    • オプションの合算処理を変更。
    • Added type method.
    • typeメソッドの追加。
    • Added command line option argument filter by command name.
    • コマンド名によるコマンドラインオプション引数のフィルターを追加。
    • Fixed so that only some options can be displayed in help.
    • helpで一部オプションのみを表示できるように修正。
  • 2019/10/11 ver1.1.0

    • Changed the return value of require to class.
    • requireの戻り値をclassに変更。
    • This makes it possible to isolate options and extend them by inheritance.
    • これによりオプションの切り分けと、継承による拡張が可能になりました。
  • 2019/10/10 ver1.0.4

    • Fixed a bug that caused an error when no entry was set anywhere.
    • エントリーをどこにもセットしていない時にエラーになるバグの修正。
  • 2019/10/09 ver1.0.0

    • First upload.
    • 初回アップロード。

License

This software is released under the MIT License.

Copyright 2019 yattaki.

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

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

1.0.0

5 years ago

0.1.0

5 years ago