#include <iostream> #include <stdlib.h> #include <stdio.h> //printf #include <unistd.h> //extern char **environ using namespace std; #include <vector> #define _GNU_SOURCE #include <getopt.h> // THIS PROGRAM CAN ./program --env=HOME // ./program -e HOME // ./program --e HOME // ./program -h // ./program --h or ./program --help extern char **environ; int main(int argc,char *argv[]) { /* cout << "Hello, World!" << endl; char *v_char = "houdini"; char *v_char_list[5]={"jack","luke"}; cout << v_char<<endl; cout << v_char_list[0]<<endl; return 0; */ /* char *v_char_list[] = {"jack","luke"}; char **v_char_point = v_char_list; while(v_char_point!=NULL) { cout<<*v_char_point<<endl; v_char_point++; } */ if (argc<=1) { printf("THIS IS A LINUX TOOLS BASED ON KATANA,PLEASE -h/--help SHOW TIPS\n"); } int opt; struct option longopts[] = { {"env",1,NULL,'e'}, {"help",0,NULL,'h'}, {"showenv",0,NULL,'s'}, {0,0,0,0} }; while ((opt=getopt_long(argc,argv,":e:h:s",longopts,NULL)) != -1 ) { switch (opt) { case 'e': { printf("get new option: %s \n", optarg); char *env_name = getenv(optarg); if (env_name) { printf("%s value is %s \n", optarg, env_name); } else { printf("The %s env do not exist\n", env_name); } break; } case 'h': { printf("\033[40;37m -h/--help : This Program helps\n \033[0m"); printf("\033[40;37m -e=value/--env=value : You want get the env value\n \033[0m"); printf("\033[40;37m -s/--showenv : show current environment \n \033[0m"); printf("\n"); break; } case 's': { char **env=environ; while(*env) { printf("%s\n",*env); env++; } break; } case '?': printf("unkown option: %c \n",optopt); break; } }; for(;optind<argc;optind++) { printf(" \"%s\" argument have no use...: \n",argv[optind]); } exit(12); }