aobatact / clap-serde

Provides a wrapper to deserialize clap app using serde.
Apache License 2.0
22 stars 3 forks source link

feat: export ENV vars command line clap2env tool #52

Open dzmitry-lahoda opened 10 months ago

dzmitry-lahoda commented 10 months ago

flow considered

  1. use config of CLI defined in TOML/YAML file
  2. parse command line parameters
  3. export CONSTANT_CASE ENV vars.
  4. so any script/app running after can access to ENV vars parsed

https://discourse.nixos.org/t/nix-bash-maxi-declaratively-configured-cli-args-to-env-vars-tool/37735

Run example.sh --some-par=42:

SOME_PAR=13 clap2env clap-config.toml $@

echo $SOME_PAR

will give

42
dzmitry-lahoda commented 10 months ago

will donate 100 USD in crypto for such tool :)

epage commented 10 months ago

I believe there is a tool that wraps clap for use in bash. Not finding it atm but on mobile.

One challenge with the suggested approach is that the tool can't modify the outside environment. It could dump a script to be sourced or it can wrap a command.

dzmitry-lahoda commented 10 months ago

I believe there is a tool that wraps clap for use in bash. Not finding it atm but on mobile.

Yeah, I searched various terms, not found yet.

I am ok with Go/C/Zig/AOT compiled bytecode - as long as startup is fast.

I just found that clap seems fits as per clap-serde crate already does half of work.

dzmitry-lahoda commented 10 months ago

One challenge with the suggested approach is that the tool can't modify the outside environment. It could dump a script to be sourced or it can wrap a command.

yeah, i see.

so it can be something like:

# bash is bad here, but kind of this to work
source < $(clap2nix clap-config.toml $@)

https://unix.stackexchange.com/questions/125110/how-do-i-source-another-processs-environment-variables

and other option try work:

# so not clear how process reuse shell called it - seems not great until there is 100% portable robust cargo to handle that
clap2nix clap-config.toml $@ -- echo $SOME_PAR