-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_arguments.c
98 lines (88 loc) · 4.79 KB
/
handle_arguments.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <getopt.h> // getopt_long (function), struct option (type)
#include <stdio.h> // printf (function), fprintf (function), stderr (macro)
#include <stdlib.h> // exit (function), EXIT_FAILURE (macro)
#include <unistd.h> // getopt (function)
#include "actions.h" // save_current_directory, go_to_directory, show_all_spots, quit_work (functions)
#include "handle_arguments.h" // handle_arguments (function prototype)
void print_help(void); // Function prototype for printing help information
void handle_arguments(int argc, char *argv[]) { // Function to process command-line arguments
int opt; // Variable to store the current option
char *directory_name = NULL; // Pointer to store the directory name
int save_mark_option_used = 0; // Flag for save-mark option usage
int all_option_used = 0; // Flag for all option usage
char *mark_name = NULL;
int delete_option_used = 0; // Flag for delete mark option
// Define long options for getopt_long
static struct option long_options[] = {
{"save-mark", required_argument, 0, 's'}, // Option to save current directory
{"delete-mark", required_argument, 0, 'd'}, // Option to save current directory
{"all-marks", no_argument, 0, 'a'}, // Option to show all saved spots
{"help", no_argument, 0, 'h'}, // Option to display help
{"version", no_argument, 0, 'v'}, // Option to display version
{"quiet", no_argument, 0, 'q'}, // Option for quiet mode
{0, 0, 0, 0} // Array terminator
};
int long_index = 0; // Index of the current option in long_options array
// Parse command-line options using getopt_long
while ((opt = getopt_long(argc, argv, "s:d:ahvq", long_options, &long_index)) != -1) {
switch (opt) { // Switch on the current option
case 's': // Save current directory with provided name
directory_name = optarg; // Store the provided directory name
save_mark_option_used = 1; // Set the flag for save-mark option
break;
case 'd':
mark_name = optarg;
delete_option_used = 1; // Set the flag for delete-mark option
break;
case 'a': // Show all saved spots
all_option_used = 1; // Set the flag for all option
break;
case 'h': // Print help information
print_help(); // Call function to print help
break;
case 'v': // Print version of nooks
print_version(); // Call function to print version
break;
case 'q': // Disable output printing (quiet mode)
quit_work(); // Call function to enable quiet mode
break;
case '?': // Handle unknown options and missing arguments
if (optopt == 's') { // If 's' option is missing an argument
fprintf(stderr, "Option -%c requires an argument.\n",
optopt); // Print error message
fprintf(stderr,
"Usage: %s [-s|--save-mark mark_name] [-a|--all-marks] [-h|--help] "
"[directory_name]\n",
argv[0]); // Print usage information
exit(EXIT_FAILURE); // Exit the program with failure status
} else { // For other unknown options
fprintf(stderr, "Unknown option '-%c'.\n", optopt); // Print error message
fprintf(stderr,
"Usage: %s [-s|--save-mark mark_name] [-d|--delete-mark mark_name] [-a|--all-marks] [-h|--help] "
"[directory_name]\n",
argv[0]); // Print usage information
exit(EXIT_FAILURE); // Exit the program with failure status
}
break;
default: // Handle unexpected cases
abort(); // Abort the program
}
}
// Handle the --all-marks or -a option
if (all_option_used) {
show_all_spots();
return;
}
// Handle the --save-mark or -s option
if (save_mark_option_used) {
save_current_directory(directory_name);
}
// Handle the --delete-mark or -d option
if (delete_option_used) {
remove_mark(mark_name);
}
// Handle non-option arguments (directory name without flag)
if (optind < argc && !save_mark_option_used) {
go_to_directory(argv[optind]);
}
}