Skip to content

Instantly share code, notes, and snippets.

@nariaki3551
Last active July 18, 2021 04:31
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/c5b26c2e223025a8b11fb3780385da34 to your computer and use it in GitHub Desktop.
Save nariaki3551/c5b26c2e223025a8b11fb3780385da34 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