I use gFlags in a c++ application, to collect command line flags:
DEFINE_string("output_dir", ".", "existing directory to dump output");
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(argc, argv, true);
...
}
This flag has a default value, so the user may choose not to provide the same on the command line. Is there any API in gFlags to know if the flag was supplied in the command line? I did not find any, so using the following hack:
DEFINE_string("output_dir", ".", "existing directory to dump output");
static bool flag_set = false;
static void CheckFlags(const int argc, char** const argv) {
for (int i = 0; i < argc; i++) {
if (string(argv[i]).find("output_dir") != string::npos) {
flag_set = true;
break;
}
}
}
int main(int argc, char** argv) {
CheckFlags(argc, argv);
gflags::ParseCommandLineFlags(argc, argv, true);
if (flag_set) {
// blah.. blah..
}
return 0;
}
After studying the gflags code in detail, I found an API gflags::GetCommandLineFlagInfoOrDie(const char* name) which returns CommandLineFlagInfo, which contains a boolean flag named is_default which is false if the flag was provided in the command line:
struct CommandLineFlagInfo {
std::string name; // the name of the flag
//...
bool is_default; // true if the flag has the default value and
// has not been set explicitly from the cmdline
// or via SetCommandLineOption
//...
};
So I do not need the hack any more:
DEFINE_string("output_dir", ".", "existing directory to dump output");
static bool flag_set = false;
int main(int argc, char** argv) {
CheckFlags(argc, argv);
gflags::ParseCommandLineFlags(argc, argv, true);
bool flag_not_set = gflags::GetCommandLineFlagInfoOrDie("output_dir").is_default;
if (!flag_not_set) {
// blah.. blah..
}
return 0;
}
Related
the current algorithm is working in boost, but it isn't working in pybind11
int main(int argc, char **argv)
{
try
{
// retrieve the filename from command line argument if there is one
auto filename = QString();
if (argc > 1)
{
filename = argv[1];
}
initPalette();
QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
srand48(time(NULL))
Py_Initialize();
PySys_SetArgvEx(0, NULL, 0);
// Disable the SIGINT handler in Python so we can Ctrl-C to exit
{
namespace py = pybind11;
auto main = py::module::import("__main__").attr("__dict__");
auto pyrun = py::exec("import signal; signal.signal(signal.SIGINT, signal.SIG_DFL)",main);
}
I am new with c++ and in a project i need to use command line argument.
I read about command line arguments i.e including
int main(int argc, char** argv)
{
}
but i have a problem declaring my filename in the source file.
i declared my input and output filename in the source file(file_process.cpp) as
const char iFilename[] ;
const char oFilename[] ;
defined the function(which uses the input file - iFilename and process the output in the oFilename) as
void file_process::process(iFilename[], oFilename[])
{
body...
}
and in the main method as :
int main(int argc, char** argv) {
iFilename[] = argv[1];
oFilename[] = argv[2];
file_process::process(iFilename[], oFilename[]);
}
earlier i hardcoded the filename to test my program without arguments in the main method and declaring the variable in the source file(file_process.cpp)as:
const char iFilename[] = "input_file.pdf";
const char oFilename[] = "output_file.txt";
and its working fine but when I am trying to take the arguments from the command line as stated above and I am not able to compile it.
Is it the right way of doing it in c++ ? I work with c# and there simply declaring in the source file like:
string iFilename = args[0];
string oFilename = args[1];
works.
I
Here is one way to do it:
int main(int argc, char** argv)
{
assert(argc >= 3);
const std::string iFilename = argv[1];
const std::string oFilename = argv[2];
file_process::process(iFilename, oFilename);
}
And you file_process::process could be:
void file_process::process(const std::string& iFilename, const std::string& oFilename)
{
body...
}
i would like to give my file a default argv, if none is given in the console. It doesn't work cause of duplicate parameter names.
if no parameter is given, i would like to use a fixed filename in the same folder.
int main(int argc, char* argv[], char* argv[1]="test.ps1")
{
std::string target = _T(argv[1]);
std::string temp= std::string("powershell.exe -command \"")+target +std::string("\"");
ShellExecuteA(0, "runas", "powershell.exe", temp.c_str, "", SW_HIDE);
}
int main(int argc, char **argv) {
std::string const default_file = "test.ps1";
std::string file = (argc < 2) ? default_file : argv[1];
doSomethingWithFile(file);
}
I have a program that requires a command line argument to run properly, but it runs even when no argument is provided. How do I ensure that an argument is provided before it runs?
int main(int argc, const char ** argv) {
std::ifstream b(argv[1]);
Word c;
c.fillWords(c.getWordsAdress(), &b);
c.printWord(c.getWordsAdress());
}
Check the argument count like this:
int main( int argc, const char* argv[] )
{
if (argc < 2)
return 1;
// your code here
}
You can just check the argument count and if it is less than 2, that means that no arguments were provided. The argument count will always have at least 1, which contains the name of the program.
int main(int argc, char** argv)
{
if(argc < 2) {
cerr << "usage: " << argv[0] << " -argument";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
#include <stdio.h>
int main(char sendbuf[100])
{
printf (sendbuf);
return 0;
}
Somehow this very basic program crashes when I try to use it, it's meant to print up whatever is typed as a parameter. If I remove the line "printf (sendbuf);" the crash goes away.
The first argument to main is the number of parameters. The second argument is an array of strings. The first element (index 0) of the second argument is the name of your program:
#include <stdio.h>
int main(int c, char **argv)
{
printf ("%s\n", c > 1 ? argv[1] : "No Argument");
return 0;
}
Your first parameter must be an integer, not a char array. Here is the right program:
#include <stdio.h>
int main(int argc, char* argv[])
{
if (argc > 1) {
printf( argv[1] );
}
else {
printf( "No arguments provided" );
}
return 0;
}
argv[0] is your program name, so argv[1] is the first parameter provided on teh command line.
C supports two forms of main function:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }
To take parameter from main, you need to change your code to:
#include <stdio.h>
int main(int argc, char* argv[])
{
if (argc > 1){
printf ("%s\n", argv[0]);
}
return 0;
}
Or use stream:
#include <iostream>
int main(int argc, char* argv[])
{
if (argc > 1){
std::cout << argv[0]) << std::endl;
}
return 0;
}
argv[0] is application name, input parameters starts from argv[1] if any.
An implementation must support the following two definitions of main:
int main() { }
int main(int argc, char* argv[]) { }
It is implementation-defined whether they support any other definitions. I don't know of any implementation that allows int main(char*) though (which is what yours is equivalent to).
This will print everything you type on the command line after the program name, even with spaces. It will not crash if you type nothing after the program name.
#include <stdio.h>
int main(int argc, char **argv)
{
for(int i=1; i<=argc; ++i) {
printf("%s\n", argv[i]);
}
}