mkideal / cli

CLI - A package for building command line app with go
MIT License
732 stars 43 forks source link

Self config allows to go with the executable file #40

Closed suntong closed 6 years ago

suntong commented 7 years ago

Using https://github.com/suntong/lang/blob/master/lang/Go/src/sys/CLI/027-global-option.go#L21 to showcase the situation:

.../lang/Go/src/sys/CLI$ 027-global-option
root
{"Host":"regexp\\s+test","Port":8080}{"Host":"regexp\\s+test","Port":8080}
[2 15]

cd ../

.../lang/Go/src/sys$ CLI/027-global-option
ERR! open 027-global-option.json: no such file or directory

If we allow such self-config .json files to reside where the executable file is, then such with-self-config executable can truly be invoked from everywhere.

suntong commented 7 years ago

Same test as above,

.../lang/Go/src/sys/CLI$ 027-global-option
root
{"Host":"regexp\\s+test","Port":8080}{"Host":"regexp\\s+test","Port":8080}
[2 15]

cd ../

.../lang/Go/src/sys$ CLI/027-global-option
root
{"Host":"regexp\\s+test","Port":8080}{"Host":"regexp\\s+test","Port":8080}
[2 15]

I.e. problem fixed -- such with-self-config executable can truly be invoked from everywhere now.

mkideal commented 6 years ago

It's not a good idea for changing ReadJSONFromFile method.

suntong commented 6 years ago

It's not a good idea for changing ReadJSONFromFile method.

Ops. OK. Your call, as long as it works as expected.

Just a precaution, os.Args[0] will be an empty string when the program is invoked from within the path.

That's why I had to do a second fix for the problem, https://github.com/go-easygen/cli/commit/926c6029618c3d3915b7b27629f864d093eb9039#diff-6314e6e9a009f03a2a8537fd10a4526aR108

Please double check. My OS is

lsb_release -a 
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04 LTS
Release:        18.04
Codename:       bionic
mkideal commented 6 years ago

os.Args[0] will be an empty string when the program is invoked from within the path

Can you write an exampe program and how to run it?

suntong commented 6 years ago