jarro2783 / cxxopts

Lightweight C++ command line option parser
MIT License
4.25k stars 590 forks source link

In Linux systems, the tilde symbol (~) cannot be automatically expanded to "/home/user1," #408

Closed warren-lei closed 1 year ago

warren-lei commented 1 year ago

I need to pass a model directory to my program by using cxxopts.hpp to parse arguments from command line. Like this: ./my_program --model_dir=~/model/ But tilde symbol (~) cannot be automatically expanded, which led to my program cannot find the correct path of the models. The tilde symbol (~) is handled as a normal string in cxxopts.hpp. For example, if the current user is "user1," then ~ should automatically expand to "/home/user1" which is the home directory of user1. I must add this codes after parsing arguments --model_dir.

void ConverToHomeDir(std::string& path) { 
        if (path[0] == '~') {
            const char* homeDir = getenv("HOME");
            if (homeDir != nullptr) {
                path.replace(0, 1, homeDir);
            } else {
                std::cerr << "HOME environment variable is not set." << std::endl;
            }
        }
    }
artpaul commented 1 year ago

@warren-lei AFAIK in most Linux systems the tilde expanded by the shell: https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html

What kind of Linux and the shell do you use?

jarro2783 commented 1 year ago

It is probably that the option was set using the = syntax and the shell won't expand that since it's in the middle of a word. If you separate the option and value it will work, or you could use $HOME.

On Mon, 9 Oct 2023, 22:47 Pavel Artemkin, @.***> wrote:

@warren-lei https://github.com/warren-lei AFAIK in most Linux systems the tilde expanded by the shell: https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html

What kind of Linux and the shell do you use?

— Reply to this email directly, view it on GitHub https://github.com/jarro2783/cxxopts/issues/408#issuecomment-1752857856, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALDQ4RR254WJP7D6RSYE63X6PP6PAVCNFSM6AAAAAA5YEPHVOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJSHA2TOOBVGY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

warren-lei commented 1 year ago

It is probably that the option was set using the = syntax and the shell won't expand that since it's in the middle of a word. If you separate the option and value it will work, or you could use $HOME. On Mon, 9 Oct 2023, 22:47 Pavel Artemkin, @.> wrote: @warren-lei https://github.com/warren-lei AFAIK in most Linux systems the tilde expanded by the shell: https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html What kind of Linux and the shell do you use? — Reply to this email directly, view it on GitHub <#408 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALDQ4RR254WJP7D6RSYE63X6PP6PAVCNFSM6AAAAAA5YEPHVOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONJSHA2TOOBVGY . You are receiving this because you are subscribed to this thread.Message ID: @.>

Yes, I set the option using the = syntax. Tilde symbol is expanded by separating the option . Thanks for help. But, if any user use my program by setting the option using the = syntax, The symbol ~ will still be treated as a normal string.

warren-lei commented 1 year ago

I set the option using the = syntax. Tilde symbol is expanded by separating the option .

I set the option using the = syntax. Problem is solved by separating the option .

--model_dir ~/models