Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions args.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,22 @@ namespace args
// prefix instead of being swallowed as a nameless long flag.
if (matchesLong && matchesShort)
{
// Identical prefixes (e.g. Windows "/") make every flag
// look both short and long. Prefer short when the next
// character is a registered short flag so /h and /ifile
// still go through ParseShort (#26).
if (longprefix == shortprefix && s.size() > shortprefix.size())
{
const char ch = s[shortprefix.size()];
for (FlagBase *flag : GetAllFlags())
{
if (flag->GetMatcher().Match(ch))
{
return OptionType::ShortFlag;
}
}
return OptionType::LongFlag;
}
return longprefix.length() >= shortprefix.length() ? OptionType::LongFlag : OptionType::ShortFlag;
}

Expand Down
55 changes: 55 additions & 0 deletions test/windows_slash_prefix.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Copyright (c) Taylor Richberger <taylor@axfive.net>
* This code is released under the license described in the LICENSE file
*/

#include "test_common.hxx"

#include <args.hxx>

#include "test_helpers.hxx"

int main()
{
// Windows-style identical "/" short and long prefixes: /h must match the
// short flag and /help the long one (#26).
{
args::ArgumentParser parser("This is a test program.");
parser.ShortPrefix("/");
parser.LongPrefix("/");
parser.LongSeparator(":");
args::Flag help(parser, "help", "display this help section", {'h', "help"});
args::Flag version(parser, "version", "show the program version", {'v', "version"});
args::ValueFlag<std::string> input(parser, "file", "the input file", {'i', "input"});
parser.ParseArgs(std::vector<std::string>{"/h", "/v", "/i", "in.txt"});
test::require(help);
test::require(version);
test::require(input);
test::require(*input == "in.txt");
}

{
args::ArgumentParser parser("This is a test program.");
parser.ShortPrefix("/");
parser.LongPrefix("/");
parser.LongSeparator(":");
args::Flag help(parser, "help", "display this help section", {'h', "help"});
args::ValueFlag<std::string> input(parser, "file", "the input file", {'i', "input"});
parser.ParseArgs(std::vector<std::string>{"/help", "/input:out.txt"});
test::require(help);
test::require(input);
test::require(*input == "out.txt");
}

// Joined short values still go through ParseShort when prefixes match.
{
args::ArgumentParser parser("This is a test program.");
parser.ShortPrefix("/");
parser.LongPrefix("/");
args::ValueFlag<std::string> input(parser, "file", "the input file", {'i', "input"});
parser.ParseArgs(std::vector<std::string>{"/ifile.txt"});
test::require(input);
test::require(*input == "file.txt");
}

return 0;
}
Loading