#env-var #command-line #env #arguments-parser #dotenv

easy_arg

EasyArg read variables from command line arguments/system envrioment/.env files

11 releases

0.2.6 May 23, 2023
0.2.5 May 9, 2023
0.1.3 May 5, 2023
0.1.2 Apr 8, 2023

#2181 in Parser implementations

Download history 8/week @ 2024-07-06

105 downloads per month

MIT/Apache

28KB
570 lines

EasyArg

EasyArg parse args from command line arguments/system envrioment/.env files

Do not use this crate for now.

TODO:

[ ] rewrite all [ ] command line support [ ] env support [ ] json support [ ] toml support


lib.rs:

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):

  1. current_working_directory/.env
  2. current_working_directory/.easy_arg.env
  3. 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

Dependencies

~2.2–3MB
~54K SLoC