是否可以更改os.Args的值,以便将测试添加到正在创建的CLI中? [重复]

This question already has an answer here:

Is it possible to change the value of os.Args globally in a Golang program? I'm writting a library like Cobra just for fun, and internally I make use of os.Args to parse the command-line arguments. I would like to do that to properly test the parser.

Currently what I am doing is manually changing the internal variable of my package, but that's a bad practice.

Thank you.

</div>

I think the answer to the previous question How to test the passing of arguments in Golang? describes how to modify the os.Args during testing.

tomasz states " the very first value in os.Args is a (path to) executable itself, so os.Args = []string{"cmd", "-user=bla"} should fix your issue. You can take a look at flag test from the standard package where they're doing something similar."