Skip to content

Commit

Permalink
docs: use gfm notes in README (#952)
Browse files Browse the repository at this point in the history
A fairly new addition to GitHub Flavored Markdown.

---------

Signed-off-by: Henry Schreiner <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Philip Top <[email protected]>
  • Loading branch information
3 people authored Dec 18, 2023
1 parent 3b58c65 commit 3bc2739
Showing 1 changed file with 66 additions and 62 deletions.
128 changes: 66 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1025,9 +1025,11 @@ option_groups. These are:
command line for a flag. The operation will throw an exception if the option
name is not valid.

> Note: if you have a fixed number of required positional options, that will
> match before subcommand names. `{}` is an empty filter function, and any
> positional argument will match before repeated subcommand names.
> [!NOTE]
>
> If you have a fixed number of required positional options, that will match
> before subcommand names. `{}` is an empty filter function, and any positional
> argument will match before repeated subcommand names.
#### Callbacks

Expand Down Expand Up @@ -1411,68 +1413,70 @@ function may already receive broken Unicode. Parsing `argv` at that point will
not give you a correct string. To fix this, you have three good options and two
bad ones:
1. Replace `argv` with `app.ensure_utf8(argv)` before any arguments are parsed.
`ensure_utf8` will do nothing on systems where `argv` is already in UTF-8
(Such as Linux or macOS) and return `argv` unmodified. On Windows, it will
discard `argv` and replace it with a correctly decoded array or arguments
from win32 API.
```cpp
int main(int argc, char** argv) {
CLI::App app;
argv = app.ensure_utf8(argv); // new argv memory is held by app
// ...
CLI11_PARSE(app, argc, argv);
}
```

2. If you pass unmodified command-line arguments to CLI11, call `app.parse()`
instead of `app.parse(argc, argv)` (or `CLI11_PARSE(app)` instead of
`CLI11_PARSE(app, argc, argv)`). The library will find correct arguments by
itself.

Note: this approach may not work on weird OS configurations, such as when the
`/proc` dir is missing on Linux systems (see also
[#845](https://github.com/CLIUtils/CLI11/issues/845)).

```cpp
int main() {
CLI::App app;
// ...
CLI11_PARSE(app);
}
```
3. Get correct arguments with which the program was originally executed using
provided functions: `CLI::argc()` and `CLI::argv()`. These three methods are
the only cross-platform ways of handling unicode correctly.
```cpp
int main() {
CLI::App app;
// ...
CLI11_PARSE(app, CLI::argc(), CLI::argv());
}
```
1\. Replace `argv` with `app.ensure_utf8(argv)` before any arguments are parsed.
`ensure_utf8` will do nothing on systems where `argv` is already in UTF-8 (Such
as Linux or macOS) and return `argv` unmodified. On Windows, it will discard
`argv` and replace it with a correctly decoded array or arguments from win32
API.
```cpp
int main(int argc, char** argv) {
CLI::App app;
argv = app.ensure_utf8(argv); // new argv memory is held by app
// ...
CLI11_PARSE(app, argc, argv);
}
```

2\. If you pass unmodified command-line arguments to CLI11, call `app.parse()`
instead of `app.parse(argc, argv)` (or `CLI11_PARSE(app)` instead of
`CLI11_PARSE(app, argc, argv)`). The library will find correct arguments by
itself.

> [!NOTE]
>
> This approach may not work on weird OS configurations, such as when the
> `/proc` dir is missing on Linux systems (see also
> [#845](https://github.com/CLIUtils/CLI11/issues/845)).
>
> ```cpp
> int main() {
> CLI::App app;
> // ...
> CLI11_PARSE(app);
> }
> ```
3\. Get correct arguments with which the program was originally executed using
provided functions: `CLI::argc()` and `CLI::argv()`. These three methods are the
only cross-platform ways of handling unicode correctly.
```cpp
int main() {
CLI::App app;
// ...
CLI11_PARSE(app, CLI::argc(), CLI::argv());
}
```
<details><summary>Bad options (click to expand)</summary><p>
4. Use the Windows-only non-standard `wmain` function, which accepts
`wchar_t *argv[]` instead of `char* argv[]`. Parsing this will allow CLI to
convert wide strings to UTF-8 without losing information.

```cpp
int wmain(int argc, wchar_t *argv[]) {
CLI::App app;
// ...
CLI11_PARSE(app, argc, argv);
}
```
5. Retrieve arguments yourself by using Windows APIs like
[`CommandLineToArgvW`](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw)
and pass them to CLI. This is what the library is doing under the hood in
`CLI::argv()`.
4\. Use the Windows-only non-standard `wmain` function, which accepts
`wchar_t *argv[]` instead of `char* argv[]`. Parsing this will allow CLI to
convert wide strings to UTF-8 without losing information.
```cpp
int wmain(int argc, wchar_t *argv[]) {
CLI::App app;
// ...
CLI11_PARSE(app, argc, argv);
}
```
5\. Retrieve arguments yourself by using Windows APIs like
[`CommandLineToArgvW`](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw)
and pass them to CLI. This is what the library is doing under the hood in
`CLI::argv()`.
</p></details>
</br>
Expand Down

0 comments on commit 3bc2739

Please sign in to comment.