cparams.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int show_help() {
  5. printf("\n");
  6. printf("Usage: cparams [options [parameters]]\n");
  7. printf("\n");
  8. printf("Mandatory options:\n");
  9. printf(" -p|--param1 [value1 | value2] (Mandatory)\n");
  10. printf("\n");
  11. printf("Options:\n");
  12. printf(" --param2 [value1 | value2]\n");
  13. printf(" --param3 (value)\n");
  14. printf("\n");
  15. printf(" -l|--list, list files in directory\n");
  16. printf(" -h|--help, Print help\n");
  17. exit(0);
  18. }
  19. int in(char **arr, int len, char *target) {
  20. int i;
  21. for(i = 0; i < len; i++) {
  22. if(strncmp(arr[i], target, strlen(target)) == 0) {
  23. return 1;
  24. }
  25. }
  26. return 0;
  27. }
  28. //int main(int argc, const char * argv[]) {
  29. int main(int argc, char **argv) {
  30. char param_1[10] = { '\0' };
  31. char param_2[10] = { '\0' };
  32. char param_3[10] = { '\0' };
  33. /* Show help if no parameter is use */
  34. if(argv[1] == NULL || strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
  35. show_help();
  36. }
  37. if(strcmp(argv[1], "-l") == 0 || strcmp(argv[1], "--list") == 0) {
  38. show_help();
  39. }
  40. /* Parse parameters */
  41. int i = 1; // start at 1, to start from first parameter used
  42. int remaining = argc - 1; // argc starts from 0, as 0 is the command
  43. printf("INFO: Parameters entered: %i\n", argc - 1);
  44. while (i < argc) {
  45. remaining--; // remove 1 parameter for parameter name
  46. if (strcmp(argv[i],"-p") == 0 || strcmp(argv[i],"--param1") == 0) {
  47. i++; // add 1 to i, to get next parameter as value
  48. strcpy(param_1, argv[i]);
  49. printf("INFO: param1 value: %s\n", param_1);
  50. } else if (strcmp(argv[i],"--param2") == 0) {
  51. i++; // add 1 to i, to get next parameter as value
  52. strcpy(param_2, argv[i]);
  53. printf("INFO: param2 value: %s\n", param_2);
  54. } else if (strcmp(argv[i],"--param3") == 0) {
  55. i++; // add 1 to i, to get next parameter as value
  56. strcpy(param_3, argv[i]);
  57. printf("INFO: param2 value: %s\n", param_3);
  58. } else {
  59. printf("ERROR: Too many arguments. Exiting");
  60. show_help();
  61. }
  62. // Statement
  63. remaining--; // remove 1 parameter for parameter value
  64. printf("INFO: Parameters remaining: %i\n", remaining);
  65. printf("INFO: Parameters checked: %i\n", i);
  66. i++; // add 1 to i, to get next parameter name
  67. }
  68. // set default values
  69. if(strlen(param_1) == 0) {
  70. printf("ERROR: parameter is mandatory. Exiting\n");
  71. show_help();
  72. }
  73. if(strlen(param_2) == 0) {
  74. strcpy(param_2, "value1");
  75. printf("INFO: Setting default parameter: %s\n", param_2);
  76. }
  77. if(strlen(param_3) == 0) {
  78. strcpy(param_3, "value1");
  79. printf("INFO: Setting default parameter: %s\n", param_3);
  80. }
  81. // parameter validation
  82. char *param_1_values[3] = { "value1", "value2", "value3" };
  83. if(!in(param_1_values, 3, param_1)) {
  84. printf("ERROR: value %s is not a valid option, valid options are: ", param_1);
  85. int values_amount = (int)( sizeof(param_1_values) / sizeof(param_1_values[0]) );
  86. for(i=0; i<values_amount; i++) {
  87. printf("%s ", param_1_values[i]);
  88. }
  89. printf("\n");
  90. exit(1);
  91. }
  92. char *param_2_values[2] = { "value1", "value2" };
  93. if(!in(param_2_values, 2, param_2)) {
  94. printf("ERROR: value %s is not a valid option, valid options are: ", param_2);
  95. int values_amount = (int)( sizeof(param_2_values) / sizeof(param_2_values[0]) );
  96. for(i=0; i<values_amount; i++) {
  97. printf("%s ", param_2_values[i]);
  98. }
  99. printf("\n");
  100. exit(1);
  101. }
  102. printf("param1 %s\n", param_1);
  103. printf("param2 %s\n", param_2);
  104. printf("param3 %s\n", param_3);
  105. return 0;
  106. }