mkideal / cli

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

Dealing with regexp in config #21

Closed suntong closed 8 years ago

suntong commented 8 years ago

Hiya,

How to put regexp in the config?

Please try your 027-global-option or mine with the following in the config file:

{
        "host": "regexp\w+test",
        "port": 8080
}

That will give an error:

ERR! invalid character 'w' in string escape code

If I escape the \ like this:

{
        "host": "regexp\\w+test",
        "port": 8080
}

the content would be wrong:

{"Help":false,"Host":"regexp\\w+test","Port":8080}
                            ^^

If I use single quote in the config file, will in turn get:

ERR! invalid character '\'' looking for beginning of value

suntong commented 8 years ago

Hmm, maybe escaping the \ was the answer,

https://play.golang.org/p/Q4CLOOBV5x

will probe more...

suntong commented 8 years ago

Confirmed. https://play.golang.org/p/eXZVSit6Bt https://play.golang.org/p/GHg_i0vM4O

Will check why it didn't work for my program. But that's a different story.

mkideal commented 8 years ago

First,

`regexp\s+test`

equivalent to

"regexp\\s+test"

Second,run https://play.golang.org/p/Q4CLOOBV5x and output is

{"Width":10,"Height":20,"Color":"regexp\\s+test","Open":false}
suntong commented 8 years ago

I know.

suntong commented 8 years ago

I meant that escaping the \ is the correct answer.