Ed94

Untitled

Oct 6th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. void PopulateFlagsAndValues(int cmdLineArgCount, char **cmdLineArguments)
  2. {
  3.     //https://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html#Using-Getopt See for docs on getopt use. (Its the GNU docs)
  4.     extern int   optind;
  5.     extern char *optarg;
  6.  
  7.     int option;
  8.  
  9. #define OperativeOptions (option = getopt(cmdLineArgCount, cmdLineArguments, "a:m:x"))
  10.  
  11. #define AddError      "Add number desired is not in supported range. Please use a number between 1-500 (Inclusive)."
  12. #define MultiplyError "Multipicative factor desired is not in supported range. Please choose a number between 1-10 (Inclusive)."
  13.  
  14.     while (OperativeOptions != -1)
  15.     {
  16.         switch (option)
  17.         {
  18.         case 'a':
  19.         {
  20.             a_flag   = true        ;
  21.             a_number = atoi(optarg);
  22.  
  23.             break;
  24.         }
  25.         case 'm':
  26.         {
  27.             m_flag   = true        ;
  28.             m_number = atoi(optarg);
  29.  
  30.             break;
  31.         }
  32.         case 'x':
  33.         {
  34.             x_flag = true;
  35.  
  36.             break;
  37.         }
  38.         default:
  39.             printf("\n%s\n", "Options switch did not find a defined case.");
  40.         }
  41.     }
  42.  
  43. #define ValueMissingError "Could not find value in arguments to use operation on. Please enter a value."
  44. #define ValueError        "Value number desired is not in supported range. Please use a number between 1-50 (Inclusive)."
  45.  
  46.     if (optind < cmdLineArgCount)
  47.     {
  48.         v_flag = true;
  49.  
  50.         value = atoi(cmdLineArguments[optind]);
  51.     }
  52.     else
  53.     {
  54.         printf("\n%s\n", ValueMissingError);
  55.     }
  56.  
  57.     if (!IsValueLegal(value))
  58.     {
  59.         printf("\n%s\n", ValueError);
  60.  
  61.         InvalidateFlags();
  62.     }
  63.     else if (!IsAddLegal(a_number))
  64.     {
  65.         printf("\n%s\n", AddError);
  66.  
  67.         InvalidateFlags();
  68.     }
  69.     else if (!IsMultiplyLegal(m_number))
  70.     {
  71.         printf("\n%s\n", MultiplyError);
  72.  
  73.         InvalidateFlags();
  74.     }
  75. }
Add Comment
Please, Sign In to add comment