* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fapi_5g / source / utils / nr5g_fapi_args.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2021 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 #include "nr5g_fapi_args.h"
20 #include <stddef.h>
21 #include <getopt.h>
22
23 static const char usage[] =
24     "                                                                               \n"
25     "    %s <APP PARAMS>                                                            \n"
26     "                                                                               \n"
27     "Application mandatory parameters:                                              \n"
28     "    --cfg FILE : configuration to load                                         \n"
29     "                                                                               \n"
30     "    --f FILE   : configuration to load                                         \n";
31
32 /* display usage */
33 static void nr5g_fapi_usage(
34     const char *prgname)
35 {
36     printf(usage, prgname);
37 }
38
39 const char *nr5g_fapi_parse_args(
40     int argc,
41     char **argv)
42 {
43     int opt;
44     int option_index;
45     const char *optname;
46     char *cfg_file = NULL;
47     char *prgname = argv[0];
48
49     static struct option lgopts[] = {
50         {"cfg", 1, 0, 0},
51         {NULL, 0, 0, 0}
52     };
53
54     while ((opt = getopt_long(argc, argv, "f", lgopts, &option_index)) != EOF) {
55         switch (opt) {
56             case 'f':
57                 printf("short opts");
58                 cfg_file = optarg;
59                 break;
60
61                 /* long options */
62             case 0:
63                 printf("long opts");
64                 optname = lgopts[option_index].name;
65                 if (0 == strcmp(optname, "cfg")) {
66                     cfg_file = optarg;
67                 }
68                 break;
69
70             default:
71                 nr5g_fapi_usage(prgname);
72                 printf("in default");
73                 return NULL;
74         }
75     }
76
77     if (cfg_file)
78         printf("config file: %s\n", cfg_file);
79     else
80         nr5g_fapi_usage(prgname);
81
82     return cfg_file;
83 }