0

I am building a command-line application using clap. I have many CLI arguments and many different parts of the application behave differently based on these arguments.

It is a hassle to make them accessible in every part though, as either every function needs to take the args as a parameter (which would introduce a lot of redundancy and unwanted code) or every struct needs to hold an instance of it, which also duplicates code and needs more memory.

In a programming language like Python, I would have a global variable that would be accessible from every point in my program - however I don't think that's possible in Rust, as the following snippet fails to compile:

const ARGS: cli::Args = cli::Args::parse();

error[E0015]: cannot call non-const fn <...cli::Args as Parser>::parse in statics

(cli::Args is a struct containing the arguments via clap's derive API).

What would be the most idiomatic way to achieve this behavior (having args available everywhere) in Rust?

leo848
  • 637
  • 2
  • 7
  • 24

0 Answers0