-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
212 lines (192 loc) · 5.96 KB
/
Copy pathexample.c
File metadata and controls
212 lines (192 loc) · 5.96 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 锯条
#define ARGON_IMPLS
#include "argon.h"
#include <stdio.h>
#include <string.h>
#define ARRAYLEN(a) (sizeof(a) / sizeof(*(a)))
int
main(int argc, char *argv[])
{
// Global options
(void) argc;
bool help = false;
// Reset subcommand options
const char *resize_input = NULL;
const char *resize_output = NULL;
int width = 0;
int height = 0;
ArgonOption resize_opts[] = {
{
.fullname = "width",
.alias = 'w',
.type = ARGON_OPTYPE_INT,
.target = &width,
.desc = "Target width",
},
{
.fullname = "height",
.alias = 'h',
.type = ARGON_OPTYPE_INT,
.target = &height,
.desc = "Target height",
},
{
.type = ARGON_OPTYPE_STRREF,
.target = (void *) &resize_input,
.desc = "input file",
},
{
.type = ARGON_OPTYPE_STRREF,
.target = (void *) &resize_output,
.desc = "output file",
},
ARGON_OPTION_SENTINEL,
};
// Convert subcommand options
const char *format = NULL;
int quality = 90;
double scale = 1.0;
bool overwrite = false;
const char *conv_input = NULL;
const char *conv_output = NULL;
const char *fmt_enums[] = {"png", "jpg", "bmp", "webp", NULL};
ArgonOption convert_opts[] = {
{
.fullname = "format",
.alias = 'f',
.type = ARGON_OPTYPE_STRREF,
.target = (void *) &format,
.desc = "Output format",
.enum_plugin = {.enums = fmt_enums, .case_sensitive = false},
},
{
.fullname = "quality",
.alias = 'q',
.type = ARGON_OPTYPE_INT,
.target = &quality,
.desc = "Quality 1-100 (default: 90)",
},
{
.fullname = "scale",
.alias = 's',
.type = ARGON_OPTYPE_FLOAT,
.target = &scale,
.desc = "Scale factor (default: 1.0)",
},
{
.fullname = "overwrite",
.alias = 0,
.type = ARGON_OPTYPE_BOOL,
.target = &overwrite,
.desc = "Overwrite existing files",
},
{
.type = ARGON_OPTYPE_STRREF,
.target = (void *) &conv_input,
.desc = "input file",
},
{
.type = ARGON_OPTYPE_STRREF,
.target = (void *) &conv_output,
.desc = "output file",
},
ARGON_OPTION_SENTINEL,
};
// Info subcommand options
const char *info_input = NULL;
bool metadata = false;
ArgonOption info_opts[] = {
{
.fullname = "metadata",
.alias = 'm',
.type = ARGON_OPTYPE_BOOL,
.target = &metadata,
.desc = "Show only metadata",
},
{
.type = ARGON_OPTYPE_STRREF,
.target = (void *) &info_input,
.desc = "input file",
},
ARGON_OPTION_SENTINEL,
};
// Global option array
ArgonOption global_opts[] = {
{
.fullname = "help",
.alias = 'h',
.type = ARGON_OPTYPE_BOOL,
.target = &help,
.desc = "Show help",
},
ARGON_OPTION_SENTINEL,
};
static const char SUB_CONVERT[] = "convert", SUB_RESIZE[] = "resize",
SUB_INFO[] = "info";
ArgonSub subcommands[] = {
{
.name = SUB_CONVERT,
.alias = 'c',
.desc = "Convert image format",
.options = convert_opts,
},
{
.name = SUB_RESIZE,
.alias = 'r',
.desc = "Resize image dimensions",
.options = resize_opts,
},
{
.name = SUB_INFO,
.alias = 'i',
.desc = "Show image information",
.options = info_opts,
},
ARGON_SUB_SENTINEL,
};
Argon argon = {
.options = global_opts,
.subs = subcommands,
.flags = ARGON_COMBINED_MATCHING | ARGON_CASE_INSENSITIVE,
};
ArgonResult r = argon_parse(&argon, argv);
if (r != ARGON_OK)
{
(void) fprintf(stderr, "Error: %s\n", argon.errmsg);
(void) fprintf(stderr, "Try 'imgtoolhelp' for usage.\n");
return 1;
}
const ArgonSub *cmd = argon_active_sub(&argon);
if (help || !cmd)
{
printf("Usage: imgtool [OPTIONS] <subcommand> [ARGS...]\n\n");
argon_print_table(&argon, stdout);
return 0;
}
// Subcommand dispatch
if (strncmp(cmd->name, SUB_CONVERT, ARRAYLEN(SUB_CONVERT) - 1) == 0)
{
printf("=== Convert ===\n");
printf(" input: %s\n", conv_input ? conv_input : "(not set)");
printf(" output: %s\n", conv_output ? conv_output : "(not set)");
printf(" format: %s\n", format ? format : "(default)");
printf(" quality: %d\n", quality);
printf(" scale: %.2f\n", scale);
printf(" overwrite: %s\n", overwrite ? "yes" : "no");
}
else if (strncmp(cmd->name, SUB_RESIZE, ARRAYLEN(SUB_RESIZE)) == 0)
{
printf("=== Resize ===\n");
printf(" input: %s\n", resize_input ? resize_input : "(not set)");
printf(" output: %s\n", resize_output ? resize_output : "(not set)");
printf(" size: %dx%d\n", width, height);
}
else if (strncmp(cmd->name, SUB_INFO, ARRAYLEN(SUB_INFO)) == 0)
{
printf("=== Info ===\n");
printf(" input: %s\n", info_input ? info_input : "(not set)");
printf(" metadata: %s\n", metadata ? "yes" : "no");
}
return 0;
}