|
-
February 7th, 2011, 03:15 PM
#1
[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.
-
February 7th, 2011, 03:34 PM
#2
Re: print out command that entered
Quotes are handled by the OS, they are lost before they are even given to your program.
-
February 7th, 2011, 03:42 PM
#3
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);
}
-
February 7th, 2011, 04:25 PM
#4
Re: print out command that entered
 Originally Posted by Martin O
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.
-
February 7th, 2011, 04:30 PM
#5
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.
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.
-
February 7th, 2011, 04:42 PM
#6
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.
-
February 7th, 2011, 04:44 PM
#7
Re: print out command that entered
 Originally Posted by dukevn
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.
-
February 8th, 2011, 01:55 PM
#8
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).
-
February 8th, 2011, 03:37 PM
#9
Re: print out command that entered
 Originally Posted by jwbarton
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|