Expand description
EasyArg
EasyArg parse args from command line arguments/system envrioment/.env files
You will read:
- how to set
- how to get
- how to alias
- how to add help description
How to set
1.Command line:
your_app -p --name=some_one -- invalid --hello "world"
will produce:
{
"name": "some_one",
"hello": "world",
"p": "true",
}
rule:
--p = "abc" ✔
--p=abc ✔
--p abc ✔
--p -- abc ✔ // p = "abc",-- will be ignored
-p ✔ // p = "true"
-p=abc ✔
--p ❌
2.system envrironment variable
export SOME_VAR=123
Then you can access it by:
let easy = EasyArg::new();
easy.get_string("SOME_VAR") // "123"
3..env files
You can use .env file in diffrent ways:
- pass
-e
/--envfile
/-envfile
args
If no envfile args, it will search (in order):
- current_working_directory/.env
- current_working_directory/.easy_arg.env
- home_directory/.easy_arg.env
If you want to customize the .env filename, use
EasyArg::new_with_env("your_file_name");
It will search:
- current_working_directory/your_file_name.env
- home_directory/your_file_name.env
.env example:
(docs.rs couldn’t display .env code properly, read it on crates.io)
DIR = abc
DIR2="dfdadfasfasf" # this is a comment
will produce
{
"OK": "true",
"DIR": "abc",
"DIR2": "dfdadfasfasf",
"HHD": "/home/xxx/abc/${NOT_EXIST}",
"GOOD": "true",
}
How to get
Note: All variables will be parsed to string.
easy
represents an instance of EasyArg
.
1. You need the raw string
easy.raw_value("KEY");
2. You need a string, event it doesn’t exist
easy.get_string("KEY");
3. panic if the string doesn’t exist
easy.must_get_string("IMPORTANT_KEY");
4. You need bool value
if you didn’t provid a value for the key, it defaults to true.
Only "false"
,"False"
,"FALSE"
,"0"
,"null"
,"NULL"
,"Null"
will be parsed to false.
easy.get_bool("KEY");
5. You need a vec
// --list=a,b,c
easy.get_vec("KEY") // the result: vec!["a", "b", "c"]
How to alias
your_app -p --s=xbox
your rust code
easy.alias("p", "PS5")
easy.to_string("PS5") // "true"
easy.to_string("p") // "true"
How to add help description
easy.desc_str("a", "description")
easy.desc_flag("b", "description")
output:
Executable: target/debug/xxx
-- a description
- b description
Structs
Enums
Functions
- read key/value from .env file