Skip to content

Commit 45cf94a

Browse files
committed
Convert texassemble to use GNU-style long options
1 parent 3f2d7fd commit 45cf94a

File tree

1 file changed

+112
-49
lines changed

1 file changed

+112
-49
lines changed

Texassemble/texassemble.cpp

+112-49
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,12 @@ namespace
138138
OPT_GIF_BGCOLOR,
139139
OPT_SWIZZLE,
140140
OPT_STRIP_MIPS,
141-
OPT_MAX
141+
OPT_FLAGS_MAX,
142+
OPT_VERSION,
143+
OPT_HELP,
142144
};
143145

144-
static_assert(OPT_MAX <= 32, "dwOptions is a unsigned int bitfield");
146+
static_assert(OPT_FLAGS_MAX <= 32, "dwOptions is a unsigned int bitfield");
145147

146148
//////////////////////////////////////////////////////////////////////////////
147149
//////////////////////////////////////////////////////////////////////////////
@@ -194,13 +196,38 @@ namespace
194196
{ L"wrap", OPT_TA_WRAP },
195197
{ L"mirror", OPT_TA_MIRROR },
196198
{ L"fl", OPT_FEATURE_LEVEL },
199+
200+
// Deprecated options (recommend using new -- alternatives)
197201
{ L"tonemap", OPT_TONEMAP },
198202
{ L"bgcolor", OPT_GIF_BGCOLOR },
199203
{ L"swizzle", OPT_SWIZZLE },
200204
{ L"stripmips", OPT_STRIP_MIPS },
205+
201206
{ nullptr, 0 }
202207
};
203208

209+
const SValue<uint32_t> g_pOptionsLong[] =
210+
{
211+
{ L"feature-level", OPT_FEATURE_LEVEL },
212+
{ L"file-list", OPT_FILELIST },
213+
{ L"format", OPT_FORMAT },
214+
{ L"gif-bg-color", OPT_GIF_BGCOLOR },
215+
{ L"height", OPT_HEIGHT },
216+
{ L"help", OPT_HELP },
217+
{ L"image-filter", OPT_FILTER },
218+
{ L"overwrite", OPT_OVERWRITE },
219+
{ L"separate-alpha", OPT_SEPALPHA },
220+
{ L"srgb-in", OPT_SRGBI },
221+
{ L"srgb-out", OPT_SRGBO },
222+
{ L"strip-mips", OPT_STRIP_MIPS },
223+
{ L"swizzle", OPT_SWIZZLE },
224+
{ L"to-lowercase", OPT_TOLOWER },
225+
{ L"tonemap", OPT_TONEMAP },
226+
{ L"version", OPT_VERSION },
227+
{ L"width", OPT_WIDTH },
228+
{ nullptr, 0 }
229+
};
230+
204231
#define DEFFMT(fmt) { L## #fmt, DXGI_FORMAT_ ## fmt }
205232

206233
const SValue<DXGI_FORMAT> g_pFormats[] =
@@ -500,7 +527,7 @@ namespace
500527

501528
static const wchar_t* const s_usage =
502529
L"Usage: texassemble <command> <options> [--] <files>\n"
503-
L"\n"
530+
L"\nCOMMANDS\n"
504531
L" cube create cubemap\n"
505532
L" volume create volume map\n"
506533
L" array create texture array\n"
@@ -518,34 +545,43 @@ namespace
518545
L" cube-from-ht create cubemap from a h-tee image\n"
519546
L" cube-from-hs create cubemap from a h-strip image\n"
520547
L" cube-from-vs create cubemap from a v-strip image\n"
521-
L"\n"
548+
L"\nOPTIONS\n"
522549
L" -r wildcard filename search is recursive\n"
523-
L" -flist <filename> use text file with a list of input files (one per line)\n"
524-
L" -w <n> width\n"
525-
L" -h <n> height\n"
526-
L" -f <format> format\n"
527-
L" -if <filter> image filtering\n"
528-
L" -srgb{i|o} sRGB {input, output}\n"
529-
L" -o <filename> output filename\n"
530-
L" -l force output filename to lower case\n"
531-
L" -y overwrite existing output file (if any)\n"
532-
L" -sepalpha resize alpha channel separately from color channels\n"
550+
L" -flist <filename>, --file-list <filename>\n"
551+
L" use text file with a list of input files (one per line)\n"
552+
L"\n"
553+
L" -w <n>, --width <n> width for output\n"
554+
L" -h <n>, --height <n> height for output\n"
555+
L" -f <format>, --format <format> pixel format for output\n"
556+
L"\n"
557+
L" -if <filter>, --image-filter <filter> image filtering\n"
558+
L" -srgb{i|o}, --srgb-in, --srgb-out sRGB {input, output}\n"
559+
L"\n"
560+
L" -o <filename> output filename\n"
561+
L" -l, --to-lowercase force output filename to lower case\n"
562+
L" -y, --overwrite overwrite existing output file (if any)\n"
563+
L"\n"
564+
L" -sepalpha, --separate-alpha resize/generate mips alpha channel separately from color channels\n"
565+
L"\n"
533566
L" -nowic Force non-WIC filtering\n"
534567
L" -wrap, -mirror texture addressing mode (wrap, mirror, or clamp)\n"
535568
L" -alpha convert premultiplied alpha to straight alpha\n"
536569
L" -dx10 Force use of 'DX10' extended header\n"
537570
L" -nologo suppress copyright message\n"
538-
L" -fl <feature-level> Set maximum feature level target (defaults to 11.0)\n"
571+
L"\n"
572+
L" -fl <feature-level>, --feature-level <feature-level>\n"
573+
L" Set maximum feature level target (defaults to 11.0)\n"
574+
L"\n"
539575
L" -tonemap Apply a tonemap operator based on maximum luminance\n"
540576
L"\n"
541577
L" (gif only)\n"
542-
L" -bgcolor Use background color instead of transparency\n"
578+
L" --gif-bg-color Use background color instead of transparency\n"
543579
L"\n"
544580
L" (merge only)\n"
545-
L" -swizzle <rgba> Select channels for merge (defaults to rgbB)\n"
581+
L" --swizzle <rgba> Select channels for merge (defaults to rgbB)\n"
546582
L"\n"
547583
L" (cube, volume, array, cubearray, merge only)\n"
548-
L" -stripmips Use only base image from input dds files\n"
584+
L" --strip-mips Use only base image from input dds files\n"
549585
L"\n"
550586
L" '-- ' is needed if any input filepath starts with the '-' or '/' character\n";
551587

@@ -775,6 +811,7 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
775811
return 0;
776812
}
777813

814+
// check for these before the command
778815
if (('-' == argv[1][0]) && ('-' == argv[1][1]))
779816
{
780817
if (!_wcsicmp(argv[1], L"--version"))
@@ -824,50 +861,76 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
824861
std::list<SConversion> conversion;
825862
bool allowOpts = true;
826863

827-
for (int iArg = 2; iArg < argc; iArg++)
864+
for (int iArg = 2; iArg < argc; ++iArg)
828865
{
829866
PWSTR pArg = argv[iArg];
830867

831-
if (allowOpts
832-
&& ('-' == pArg[0]) && ('-' == pArg[1]))
868+
if (allowOpts && (('-' == pArg[0]) || ('/' == pArg[0])))
833869
{
834-
if (pArg[2] == 0)
835-
{
836-
// "-- " is the POSIX standard for "end of options" marking to escape the '-' and '/' characters at the start of filepaths.
837-
allowOpts = false;
838-
}
839-
else if (!_wcsicmp(pArg, L"--version"))
840-
{
841-
PrintLogo(true, g_ToolName, g_Description);
842-
return 0;
843-
}
844-
else if (!_wcsicmp(pArg, L"--help"))
870+
uint64_t dwOption = 0;
871+
PWSTR pValue = nullptr;
872+
873+
if (('-' == pArg[0]) && ('-' == pArg[1]))
845874
{
846-
PrintUsage();
847-
return 0;
875+
if (pArg[2] == 0)
876+
{
877+
// "-- " is the POSIX standard for "end of options" marking to escape the '-' and '/' characters at the start of filepaths.
878+
allowOpts = false;
879+
continue;
880+
}
881+
else
882+
{
883+
pArg += 2;
884+
885+
for (pValue = pArg; *pValue && (':' != *pValue) && ('=' != *pValue); ++pValue);
886+
887+
if (*pValue)
888+
*pValue++ = 0;
889+
890+
dwOption = LookupByName(pArg, g_pOptionsLong);
891+
892+
if (dwOption == OPT_VERSION)
893+
{
894+
PrintLogo(true, g_ToolName, g_Description);
895+
return 0;
896+
}
897+
else if (dwOption == OPT_HELP)
898+
{
899+
PrintUsage();
900+
return 0;
901+
}
902+
}
848903
}
849904
else
850905
{
851-
wprintf(L"Unknown option: %ls\n", pArg);
852-
return 1;
853-
}
854-
}
855-
else if (allowOpts
856-
&& (('-' == pArg[0]) || ('/' == pArg[0])))
857-
{
858-
pArg++;
859-
PWSTR pValue;
906+
pArg++;
907+
908+
for (pValue = pArg; *pValue && (':' != *pValue) && ('=' != *pValue); ++pValue);
909+
910+
if (*pValue)
911+
*pValue++ = 0;
860912

861-
for (pValue = pArg; *pValue && (':' != *pValue); pValue++);
913+
dwOption = LookupByName(pArg, g_pOptions);
862914

863-
if (*pValue)
864-
*pValue++ = 0;
915+
if (!dwOption)
916+
{
917+
if (LookupByName(pArg, g_pOptionsLong))
918+
{
919+
wprintf(L"ERROR: did you mean `--%ls` (with two dashes)?\n", pArg);
920+
return 1;
921+
}
922+
}
923+
}
865924

866-
const uint32_t dwOption = LookupByName(pArg, g_pOptions);
925+
if (!dwOption)
926+
{
927+
wprintf(L"ERROR: Unknown option: `%ls`\n\nUse %ls --help\n", pArg, g_ToolName);
928+
return 1;
929+
}
867930

868-
if (!dwOption || (dwOptions & (1 << dwOption)))
931+
if (dwOptions & (1 << dwOption))
869932
{
870-
PrintUsage();
933+
wprintf(L"ERROR: Duplicate option: `%ls`\n\n", pArg);
871934
return 1;
872935
}
873936

0 commit comments

Comments
 (0)