-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathoptions.c
73 lines (60 loc) · 2.31 KB
/
options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License. */
#include <getopt.h>
#include <stdlib.h>
#include <memory.h>
#include <applibs/log.h>
#include "options.h"
// Usage text for command line arguments in application manifest.
static const char *cmdLineArgsUsageText =
"The command line arguments for the application shoud be set in app_manifest.json as below:\n"
"\" CmdArgs \": [\"--Hostname\", \"<your_event_grid_mqtt_hostname>\"]\n";
static char *hostname = NULL; // Azure Event Grid Hostname.
static const char *eventGridPlaceholder = "<your_event_grid_mqtt_hostname>";
static ExitCode ValidateUserConfiguration(void);
/// <summary>
/// Parse the command line arguments given in the application manifest.
/// </summary>
ExitCode Options_ParseArgs(int argc, char *argv[])
{
int option = 0;
static const struct option cmdLineOptions[] = {
{.name = "Hostname", .has_arg = required_argument, .flag = NULL, .val = 'h'},
{.name = NULL, .has_arg = 0, .flag = NULL, .val = 0}};
// Loop over all of the options.
while ((option = getopt_long(argc, argv, "h:", cmdLineOptions, NULL)) != -1) {
// Check if arguments are missing. Every option requires an argument.
if (optarg != NULL && optarg[0] == '-') {
Log_Debug("WARNING: Option %c requires an argument\n", option);
continue;
}
switch (option) {
case 'h':
Log_Debug("Hostname: %s\n", optarg);
hostname = optarg;
break;
default:
// Unknown options are ignored.
break;
}
}
return ValidateUserConfiguration();
}
char *Options_GetAzureEventGridHostname(void)
{
return hostname;
}
static ExitCode ValidateUserConfiguration(void)
{
ExitCode validationExitCode = ExitCode_Success;
if (hostname == NULL) {
validationExitCode = ExitCode_Validate_Hostname;
Log_Debug(cmdLineArgsUsageText);
} else if (strcmp(hostname, eventGridPlaceholder) == 0) {
validationExitCode = ExitCode_Validate_Hostname;
Log_Debug("Replace \"<your_event_grid_mqtt_hostname>\" with the value of Event Grid MQTT hostname in app_manifest.json.");
} else {
Log_Debug("Azure Event Grid Hostname %s\n", hostname);
}
return validationExitCode;
}