CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2009
    Posts
    89

    [RESOLVED] print out command that entered

    Hi folks,

    I need to print out the command that user enters exactly as it was entered, and this seem-to-be-simple question really bothers me. I thought there should be a simple solution for that, but honestly I have not gotten it to work yet .

    My code is follow:
    Code:
    // testCMD.cpp
    #include <iostream>
    #include <string>
    using namespace std;
    int main(int argc,char *argv[])
    {
        cout << argv[0] << " ";
        for (int i=1;i<argc;i++) cout << argv[i] << " ";
        cout << endl;
        return(0);
    }
    and the result:
    Code:
    $ g++ testCMD.cpp -o testCMD
    $ ./testCMD -aCMD1 -bYES -c '--more -here' -d "-m again"
    ./testCMD -aCMD1 -bYES -c --more -here -d -m again 
    [nguyenck@kcServ:/Volumes/Data/tools/test]$
    As you can see, the code does print out command entered, but not exactly as it should be.

    Anybody has any suggestion for me?

    Thanks,

    D.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: print out command that entered

    Quotes are handled by the OS, they are lost before they are even given to your program.

  3. #3
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: print out command that entered

    Printing all the argv elements on the same line separated by space doesn't really clarify whats going on for you. Instead try this, it will make it clearer (code added in red):

    Code:
    // testCMD.cpp
    #include <iostream>
    #include <string>
    using namespace std;
    int main(int argc,char *argv[])
    {
        cout << argv[0] << " ";
        for (int i=1;i<argc;i++) cout << "<" << argv[i] << "> ";
        cout << endl;
        return(0);
    }

  4. #4
    Join Date
    Dec 2009
    Posts
    89

    Re: print out command that entered

    Quote Originally Posted by Martin O View Post
    Printing all the argv elements on the same line separated by space doesn't really clarify whats going on for you. Instead try this, it will make it clearer (code added in red):

    Code:
    // testCMD.cpp
    #include <iostream>
    #include <string>
    using namespace std;
    int main(int argc,char *argv[])
    {
        cout << argv[0] << " ";
        for (int i=1;i<argc;i++) cout << "<" << argv[i] << "> ";
        cout << endl;
        return(0);
    }
    Thanks Martin for your suggestion. The reason why I need the printout to be exact as entered is that I want to make it easier for user when he wants to re-run the program, he can simply copy and paste. Making output clearer like that (or the similar like print out argument line by line etc...) does not really help.

    The following works better for me
    Code:
    // testCMD.cpp
    #include <iostream>
    #include <string>
    using namespace std;
    int main(int argc,char *argv[]) {
        cout << argv[0] << " ";
        for (int i=1;i<argc;i++) {
        	if ( ((string) argv[i]).find(' ') == string::npos ) cout << argv[i] << " ";
        	else cout << "'" << argv[i] << "' ";
        }
        cout << endl;
        return 0;
    }
    output
    Code:
    $ g++ testCMD.cpp -o testCMD
    $ ./testCMD -aCMD1 -bYES -c '--more -here' -d "-m again"
    ./testCMD -aCMD1 -bYES -c '--more -here' -d '-m again'
    but I still do not like it very much since.

  5. #5
    Join Date
    Dec 2009
    Posts
    89

    Re: print out command that entered

    Quote Originally Posted by ninja9578 View Post
    Quotes are handled by the OS, they are lost before they are even given to your program.
    If it is the case, then I think I would have no choice other than trying to make it usable and as clear as possible.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: print out command that entered

    You can do a strchr or find for a space. If a space exists, then it obviously must have been quoted in the params, so you can just insert quotes where you need them.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: print out command that entered

    Quote Originally Posted by dukevn View Post
    Thanks Martin for your suggestion. The reason why I need the printout to be exact as entered is that I want to make it easier for user when he wants to re-run the program, he can simply copy and paste. Making output clearer like that (or the similar like print out argument line by line etc...) does not really help.
    The suggestion was not designed to meet your goal; it was to help you understand what the program was doing.

    One possibility to meet your goal is to test whether any of the argv elements contains whitespace, and print it out surrounded by quotes if so.

  8. #8
    Join Date
    Jan 2001
    Posts
    253

    Re: print out command that entered

    Originally Posted by ninja9578
    Quotes are handled by the OS, they are lost before they are even given to your program.
    This isn't necessarily true. When running on Windows, the command line is parsed by the startup code of the executable. This is parsed into the familiar argc/argv before the runtime calls main().

    When using Visual C++, the command line parsing done by the startup code follows the rules as documented in http://msdn.microsoft.com/en-us/library/17w5ykft.aspx. This removes the quotes, but it isn't done by the OS.

    On Windows, the function GetCommandLine() can be used to get the original command line. No guesses about whether to insert quotes are needed.

    Unfortunately, if you are running on another OS, it isn't as easy to get the original command line (but if you google for it, you may find a way to do it).

  9. #9
    Join Date
    Dec 2009
    Posts
    89

    Re: print out command that entered

    Quote Originally Posted by jwbarton View Post
    On Windows, the function GetCommandLine() can be used to get the original command line. No guesses about whether to insert quotes are needed.

    Unfortunately, if you are running on another OS, it isn't as easy to get the original command line (but if you google for it, you may find a way to do it).
    Well, I originally expected something as simple as that command when posting the question. If there is none, and that it really needs some complicated way to archive that, then I am fine with the quote-guessing method.

    Thanks you guys for all of the comments and suggestions,

    D.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured