Skip to content

Instantly share code, notes, and snippets.

@nariaki3551
Created July 24, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nariaki3551/251f67838f033b96c12d55092b559cec to your computer and use it in GitHub Desktop.
Save nariaki3551/251f67838f033b96c12d55092b559cec to your computer and use it in GitHub Desktop.
#include <iostream>
#include <unistd.h> // getopt
int main(int argc, char* argv[])
{
int a = 0;
int b = 0;
bool c = false;
// first argument parsing
int opt;
for(int i = 0; i < argc; i++) std::cout << argv[i] << std::endl;
while((opt=getopt(argc,argv,"a:c"))!=-1)
{
switch(opt)
{
case 'a':
a = atoi(optarg);
break;
case 'c':
c = true;
break;
}
}
// second argment parsng
for(int i = 0; i < argc; i++) std::cout << argv[i] << std::endl;
optind = 1;
while((opt=getopt(argc,argv,"a:b:c"))!=-1)
{
switch(opt)
{
case 'a':
a = atoi(optarg);
break;
case 'b':
b = atoi(optarg);
break;
case 'c':
c = true;
break;
}
}
std::cout << "a = " << a << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "c = " << c << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment