shargs-example-sync-deepthought-config-env-argv is a sample application of shargs 🦈.
See the shargs github repository for more details!
$ git clone https://github.com/Yord/shargs-example-sync-deepthought-config-env-argv.git
$ cd shargs-example-sync-deepthought-config-env-argv
$ npm i
$ chmod +x ./deepThoughtThis repository is an example how to use several layers of configuration with shargs.
The program first reads configurations from config.json, then from environment variables,
and finally from command-line arguments.
Later configurations override earlier ones.
The program can be found in the deepThought script.
The example may be run with different parameters:
Providing a --help flag:
$ ./deepThought --helpOr providing the DT_HELP=true environment variable:
$ DT_HELP=true ./deepThoughtPrints the following text to the console:
deepThought [-a|--answer] [-h|--help]
deepThought ask [--format] [--no-html] [-h|--help] (<question>)
-a, --answer=<number> The answer. [default: 42]
-h, --help Print this help message and exit.
ask Ask a question. [required]
--format=<json|xml> Respond either with json or xml. [default: json]
--no-html Removes HTML tags.
-h, --help Print this help message and exit.
<question> State your question. [required]
Deep Thought was created to come up with the Answer to The Ultimate Question of
Life, the Universe, and Everything. Providing ask --help:
$ ./deepThought ask --helpOr providing DT_ASK__HELP=true:
$ DT_ASK__HELP=true ./deepThoughtPrints text in a different usage format:
deepThought ask [--format] [--no-html] [-h|--help] (<question>)
--format=<json|xml> [default: json]
Respond either with json or xml.
--no-html
Removes HTML tags.
-h, --help
Print this help message and exit.
<question> [required]
State your question.
Deep Thought was created to come up with the Answer to The Ultimate Question of
Life, the Universe, and Everything. Providing more complex arguments:
$ DT_ANSWER=43 DT_ASK__NO_HTML=true ./deepThought ask --no-html "What is life?"Prints the following text to the console:
The answer is: 43
{
"answer": 43,
"ask": {
"format": "json",
"_": [],
"html": false,
"question": "What is life?"
},
"_": []
}Please note, the following:
{"format": "json"}comes fromconfig.jsonand is neither overridden by environment variables nor command-line arguments.{"answer": 43}comes fromDT_ANSWER=43, which overrides{"answer": 42}fromconfig.json.{"html": false}comes fromask --no-html, which overridesDT_ASK__NO_HTML=true.{"question": "What is life?"}comes fromask "What is life?".
Providing no ask command:
$ ./deepThought --answer 23Prints the following error:
RequiredOptionMissing: An option that is marked as required has not been provided.Providing a wrong --format:
$ ./deepThought ask --format csvPrints the following error:
RequiredOptionMissing: An option that is marked as required has not been provided.
ValueRestrictionsViolated: A value lies outside the allowed values of an option.Please report issues in the shargs tracker!
shargs-example-sync-deepthought-config-env-argv is MIT licensed.