Commit graph

47 commits

Author SHA1 Message Date
Shannon Booth
88b343061e LibCore: Make short_name optional for ArgsParser
This allows us to not need to pass through a 0 in many cases for
options which do not support a short value.
2024-04-22 08:10:08 +02:00
Shannon Booth
abf35f5bd6 LibCore: Support Optional<StringView> as an argument to ArgsParser
While StringView does have a null state, we have been moving away from
this in our other String classes. To represent a StringView not being
given at all on the commandline, use an Optional.
2024-03-03 08:56:00 +01:00
Ollrogge
f784122703 LibCore: Refactor template definitions to header in ArgsParser
This allows for automatic template instatiation and remove the need to
explicitly instantiate the template functions for different types.
2024-01-29 13:58:37 +00:00
Ali Mohammad Pur
5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30
Gurkirat Singh
ab2d8c092e LibCore: Implement handler for Vector<String> positional arguments 2023-10-30 10:39:59 +00:00
Sam Atkins
f71d74ed65 LibCore: Add ArgsParser::add_option for setting enum values from a flag
Previously, argument-less options could only set a boolean to true. This
lets them also set an enum variable to a specific value, as is currently
done by the `ls` utility.
2023-09-24 23:41:22 +02:00
Timothy Flynn
c911781c21 Everywhere: Remove needless trailing semi-colons after functions
This is a new option in clang-format-16.
2023-07-08 10:32:56 +01:00
Carwyn Nelson
a2591bc5fa LibCore: Make ArgParser::Arg::accept_value return ErrorOr<bool>
Much like the previous commit, this commit makes the
ArgParser::Arg::accept_value callback return an ErrorOr<bool> instead of
just a bool.

The aim of this is to make argument parsing more robust, especially
with the newer String api that returns an ErrorOr for many functions.
2023-07-04 04:26:58 +03:30
Carwyn Nelson
abbfb00a02 LibCore: Make ArgParser::Option::accept_value return ErrorOr<bool>
This commit makes the `ArgParser::Option::accept_value` property more
error-safe by ensuring it returns an ErrorOr<bool> instead of just a
bool.
2023-07-04 04:26:58 +03:30
Carwyn Nelson
e247da507f LibCore: Add new String variants to ArgsParser
This commit adds the ability to use the String class with `add_option`
and `add_positional_argument`.

This should help with the transition away from DeprecatedString.
2023-07-03 02:05:32 +02:00
Fabian Dellwing
4ebc7306da LibCore: Implement integral ArgsParser positionals through a template
This matches the behaviour of options.
2023-04-30 21:12:46 +03:30
Ali Mohammad Pur
23c514cda3 LibCore+Userland: Remove ArgsParser::add*(Vector<char const*>&)
This finally makes ArgsParser's value API free of silly char*'s!
2023-03-01 10:47:19 +01:00
Ali Mohammad Pur
500044906d LibCore+Everywhere: Remove ArgsParser::add*(char const*&)
This is not guaranteed to always work correctly as ArgsParser deals in
StringViews and might have a non-properly-null-terminated string as a
value. As a bonus, using StringView (and DeprecatedString where
necessary) leads to nicer looking code too :^)
2023-03-01 10:47:19 +01:00
Ali Mohammad Pur
db886fe18b Userland+AK: Stop using getopt() for ArgsParser
This commit moves the implementation of getopt into AK, and converts its
API to understand and use StringView instead of char*.
Everything else is caught in the crossfire of making
Option::accept_value() take a StringView instead of a char const*.

With this, we must now pass a Span<StringView> to ArgsParser::parse(),
applications using LibMain are unaffected, but anything not using that
or taking its own argc/argv has to construct a Vector<StringView> for
this method.
2023-02-28 15:52:24 +03:30
Ali Mohammad Pur
6e5ba82929 LibCore: Make ArgsParser tolerate Main::Arguments with only .strings
This currently allocates in .parse(), but that's better than making the
caller do the exact same before passing us the values.

Note that this is only temporary to aid in conversion, a future commit
will remove this and switch to requiring the users to allocate the
vector instead.
2023-02-28 15:52:24 +03:30
MacDue
63b11030f0 Everywhere: Use ReadonlySpan<T> instead of Span<T const> 2023-02-08 19:15:45 +00:00
Moustafa Raafat
ae2abcebbb Everywhere: Use C++ concepts instead of requires clauses 2022-12-09 11:25:30 +00:00
Linus Groh
6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00
Linus Groh
d26aabff04 Everywhere: Run clang-format 2022-12-03 23:52:23 +00:00
Liav A
7d7127b463 LibCore: Add add_option aggregating the same option multiple times
This could be used in a scenario when it is expected that a user program
will be invoked with a specific option multiple times, for example:
"program --a-option=example --a-option=anotherexample ..."
2022-11-26 12:42:15 -07:00
Tim Schumacher
0f66963a56 LibCore: Implement integral ArgsParser options through a template
This keeps us from having to duplicate code for each data type we add.
2022-07-21 16:37:04 +02:00
Tim Schumacher
a85b728768 LibCore: Introduce support for optional ArgsParser option values 2022-07-14 00:24:24 +01:00
Tim Schumacher
3d51642037 LibCore: Replace the ArgsParser option argument setting with an enum
Replacement conditions for `requires_argument` have been chosen based
on what would be most convenient for implementing an eventual optional
argument mode.
2022-07-14 00:24:24 +01:00
Sam Atkins
3e5b1b1dbc LibCore: Add ArgsParser::add_positional_argument(Vector<String>&, ...) 2022-04-11 21:09:42 +02:00
sin-ack
cbc89632b9 LibCore+Base: Add way to hide ArgsParser opt.s from specific usage texts
This adds the ability to hide certain options from certain help texts.
`--complete` is always hidden, whereas `--help` and `--version` are
hidden from Markdown help text only.
Note that in all cases these three options are hidden from the short
usage line.
2022-04-06 11:39:08 +02:00
Kenneth Myhra
23a87a41ff LibCore: Add Vector<size_t> variant of add_option()
This adds a Vector<size_t> variant of add_option(). The signature
includes a separator parameter which defaults to ','.
2022-03-27 08:33:24 -04:00
Ali Mohammad Pur
da12acfa78 LibCore: Allow marking options as 'hidden' in ArgsParser
This removes them from the main invocation example in --help, as well as
hides them from autocomplete results (we were previously special-casing
"help" and "version").
2022-03-26 21:34:56 +04:30
Ali Mohammad Pur
fc4d36ccd0 LibCore: Make ArgsParser perform some completion if passed --complete
This makes it possible to autocomplete flags and options via the Shell.
2022-03-26 21:34:56 +04:30
brapru
24da85d059 ArgsParser: Add support for Optional<size_t> 2022-02-17 09:08:40 -05:00
mjz19910
14a267347c LibCore: Add ArgsParser::add_option for Optional double 2022-01-21 01:52:22 +01:00
Andreas Kling
f1cc3d0fc4 Userland: Use Core::ArgsParser's Vector<StringView> API everywhere
...and remove the Vector<String> variant since there are no remaining
users of this API.
2021-11-26 23:27:57 +01:00
Andreas Kling
395ba619d8 LibCore: Add Vector<StringView> variant of add_positional_argument() 2021-11-26 23:27:57 +01:00
Mustafa Quraish
7720644ae2 LibCore: Let ArgsParser::parse() accept Main::Arguments
Instead of manually passing in `arguments.argc` and `arguments.argv`,
we can now just pass in the arguments directly.
2021-11-22 21:13:42 -08:00
Ben Wiederhake
0372f051e9 LibCore: Enable emitting markdown from ArgsParser
ArgsParser will now automatically look for an environment variable
'ARGSPARSER_EMIT_MARKDOWN', and if it is set to exactly the string "1"
(i.e. mere presence or values like "ON" or "yes" are not enough), then
ArgsParser will emit a Markdown-formatted help message, instead of the
regular help message designed for consumption through a terminal.
2021-11-01 21:12:58 +01:00
Jean-Baptiste Boric
1b3090bf3c LibCore: Add ignored options to ArgsParser 2021-10-23 15:06:33 +02:00
Nico Weber
f25be94487 LibCore: Make --version print same version as in LibGUI's About dialogs
Making every binary's behavior depend on the current git hash seems a
bit questionable to me, but we should be self-consistent about this.
2021-08-15 01:22:45 +02:00
Nico Weber
1f71d7bf22 LibCore: Add --version flag to ArgsParser
`--version` always prints "git" for now.

The motivation is that the neofetch port calls `Shell --version` and
adds the output to its output. And if `Shell --version` prints a long
error message about it not knowing the flag, neofetch's output looks a
bit ugly. Per Discord discussion, just add the flag to ArgsParser
instead of only to Shell.
2021-08-14 22:46:34 +04:30
Aziz Berkay Yesilyurt
7db3e962f3 LibCore: Add unsigned option to ArgsParser
Unsigned options are used to return underflowed values for negative
inputs. With this change, we can verify that unsigned options only
accept unsigned inputs as arguments.
2021-07-07 20:05:43 +04:30
Jelle Raaijmakers
d7126fbbc2 LibCore/ArgsParser: Learn how to stop on first non-option
We need this for utilities like `env`, that do not gain anything by
parsing the options passed to the command they are supposed to
execute.
2021-06-08 11:30:58 +02:00
Jelle Raaijmakers
250f8eccf3 LibCore: Support fine-grained failure behavior for ArgsParser 2021-06-08 11:30:58 +02:00
Andreas Kling
313e53dca0 LibCore: Add StringView overloads for Core::ArgsParser
These allow you to get StringView wrappers around on-stack string
arguments, which seems pretty nice. :^)
2021-06-01 09:02:43 +02:00
Andreas Kling
695cdf7bc0 LibCore: Support Vector<String> positional arguments
We already supported Vector<char const*>, so let's add Vector<String>
as well. :^)
2021-04-29 11:10:06 +02:00
Brian Gianforcaro
1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Andreas Kling
f4a15c8d42 LibCore: Convert String::format() => String::formatted() 2021-04-21 23:49:02 +02:00
DexesTTP
b30f5dc8b5 LibCore: Add ArgsParser::add_option(String&)
The API existed for add_positional_argument, but not for named arguments.
2021-04-18 22:42:10 +02:00
speles
63a846a2ac LibCore: Add String variant for ArgsParser::add_positional_argument
This is basically the same version as const char *, but it's a nice
helper that allows us to handle strings without casting.
2021-03-07 11:00:36 +01:00
Andreas Kling
13d7c09125 Libraries: Move to Userland/Libraries/ 2021-01-12 12:17:46 +01:00
Renamed from Libraries/LibCore/ArgsParser.h (Browse further)