Skip to content

Commit cd1d58b

Browse files
Replace handwritten documentation with Qt documentation (#1297)
* cxx-qt: import documentation from Qt * cxx-qt-lib: import documentation from Qt * cxx-qt-lib-extras: import documentation from Qt * cxx-qt-lib: add #[doc(hidden)] to macros * cxx-qt-lib: add missing doc headers * cxx-qt-lib-extras: fix typo * cxx-qt-lib: run cargo fmt * cxx-qt-lib: fix typo --------- Co-authored-by: Leon Matthes <[email protected]>
1 parent 638f511 commit cd1d58b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2181
-1336
lines changed

crates/cxx-qt-lib-extras/src/core/qcommandlineoption.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55

66
use cxx::{type_id, ExternType};
7+
use cxx_qt_lib::{QString, QStringList};
78
use std::mem::MaybeUninit;
89

910
#[cxx::bridge]
@@ -26,22 +27,36 @@ mod ffi {
2627
/// Returns the names set for this option.
2728
fn names(self: &QCommandLineOption) -> QStringList;
2829

29-
/// Sets the default value used for this option to defaultValue.
30+
/// Sets the default value used for this option to `default_value`.
31+
///
32+
/// The default value is used if the user of the application does not specify the option on the command line.
33+
///
34+
/// If `default_value` is empty, the option has no default values.
3035
#[rust_name = "set_default_value"]
31-
fn setDefaultValue(self: &mut QCommandLineOption, value: &QString);
36+
fn setDefaultValue(self: &mut QCommandLineOption, default_value: &QString);
3237

33-
/// Sets the list of default values used for this option to defaultValues.
38+
/// Sets the list of default values used for this option to `default_values`.
39+
///
40+
/// The default values are used if the user of the application does not specify the option on the command line.
3441
#[rust_name = "set_default_values"]
35-
fn setDefaultValues(self: &mut QCommandLineOption, values: &QStringList);
42+
fn setDefaultValues(self: &mut QCommandLineOption, default_values: &QStringList);
3643

37-
/// Sets the description used for this option to description.
44+
/// Sets the description used for this option to `description`.
3845
/// It is customary to add a "." at the end of the description.
46+
///
47+
/// The description is used by [QCommandLineParser::showHelp](https://doc.qt.io/qt/qcommandlineparser.html#showHelp)().
3948
#[rust_name = "set_description"]
4049
fn setDescription(self: &mut QCommandLineOption, description: &QString);
4150

42-
/// Sets the name of the expected value, for the documentation, to valueName.
51+
/// Sets the name of the expected value, for the documentation, to `value_name`.
52+
///
53+
/// Options without a value assigned have a boolean-like behavior: either the user specifies `–option` or they don't.
54+
///
55+
/// Options with a value assigned need to set a name for the expected value, for the documentation of the option in the help output. An option with names `o` and `output`, and a value name of file will appear as `-o, --output <file>`.
56+
///
57+
/// Call [`QCommandLineParser::value`](crate::QCommandLineParser::value) if you expect the option to be present only once, and [`QCommandLineParser::values`](crate::QCommandLineParser::values) if you expect that option to be present multiple times.
4358
#[rust_name = "set_value_name"]
44-
fn setValueName(self: &mut QCommandLineOption, valueName: &QString);
59+
fn setValueName(self: &mut QCommandLineOption, value_name: &QString);
4560

4661
/// Returns the name of the expected value.
4762
#[rust_name = "value_name"]
@@ -70,35 +85,38 @@ mod ffi {
7085
}
7186
}
7287

88+
/// The `QCommandLineOption` class defines a possible command-line option.
89+
///
90+
/// Qt Documentation: [QCommandLineOption](https://doc.qt.io/qt/qcommandlineoption.html#details)
7391
#[repr(C)]
7492
pub struct QCommandLineOption {
7593
_space: MaybeUninit<usize>,
7694
}
7795

7896
impl Clone for QCommandLineOption {
79-
/// Constructs a copy of other.
97+
/// Constructs a copy of this `QCommandLineOption`.
8098
fn clone(&self) -> Self {
8199
ffi::qcommandlineoption_init_from_qcommandlineoption(self)
82100
}
83101
}
84102

85103
impl Drop for QCommandLineOption {
86-
/// Destroys the qcommandlineoption.
104+
/// Destroys the `QCommandLineOption`.
87105
fn drop(&mut self) {
88106
ffi::qcommandlineoption_drop(self)
89107
}
90108
}
91109

92-
impl From<&ffi::QString> for QCommandLineOption {
110+
impl From<&QString> for QCommandLineOption {
93111
/// Constructs a command line option object with the name name.
94-
fn from(name: &ffi::QString) -> Self {
112+
fn from(name: &QString) -> Self {
95113
ffi::qcommandlineoption_init_from_qstring(name)
96114
}
97115
}
98116

99-
impl From<&ffi::QStringList> for QCommandLineOption {
117+
impl From<&QStringList> for QCommandLineOption {
100118
/// Constructs a command line option object with the name name.
101-
fn from(names: &ffi::QStringList) -> Self {
119+
fn from(names: &QStringList) -> Self {
102120
ffi::qcommandlineoption_init_from_qstringlist(names)
103121
}
104122
}

crates/cxx-qt-lib-extras/src/core/qcommandlineparser.rs

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,36 @@
33
//
44
// SPDX-License-Identifier: MIT OR Apache-2.0
55
use cxx::{type_id, ExternType};
6+
use cxx_qt_lib::{QString, QStringList};
67
use std::mem::MaybeUninit;
78

89
#[cxx::bridge]
910
mod ffi {
10-
/// The type of image format available in Qt.
11+
/// This enum describes the way the parser interprets options that occur after positional arguments.
1112
#[repr(i32)]
1213
#[namespace = "rust::cxxqtlib1"]
1314
#[derive(Debug)]
1415
enum QCommandLineParserOptionsAfterPositionalArgumentsMode {
15-
/// application argument --opt -t is interpreted as setting the options opt and t,
16-
/// just like application --opt -t argument would do. This is the default parsing mode.
17-
/// In order to specify that --opt and -t are positional arguments instead, the user can use --,
18-
/// as in application argument -- --opt -t.
16+
/// `application argument --opt -t` is interpreted as setting the options `opt` and `t`,
17+
/// just like `application --opt -t argument` would do. This is the default parsing mode.
18+
/// In order to specify that `--opt` and `-t` are positional arguments instead, the user can use `--`,
19+
/// as in `application argument -- --opt -t`.
1920
ParseAsOptions,
20-
/// application argument --opt is interpreted as having two positional arguments, argument and --opt.
21+
/// `application argument --opt` is interpreted as having two positional arguments, `argument` and `--opt`.
2122
/// This mode is useful for executables that aim to launch other executables (e.g. wrappers, debugging tools, etc.)
2223
/// or that support internal commands followed by options for the command. argument is the name of the command,
2324
/// and all options occurring after it can be collected and parsed by another command line parser, possibly in another executable.
2425
ParseAsPositionalArguments,
2526
}
2627

28+
/// This enum describes the way the parser interprets command-line options that use a single dash followed by multiple letters, as `-abc`.
2729
#[repr(i32)]
2830
#[namespace = "rust::cxxqtlib1"]
2931
#[derive(Debug)]
3032
enum QCommandLineParserSingleDashWordOptionMode {
33+
/// `-abc` is interpreted as `-a -b -c`, i.e. as three short options that have been compacted on the command-line, if none of the options take a value. If `a` takes a value, then it is interpreted as `-a bc`, i.e. the short option a followed by the value `bc`. This is typically used in tools that behave like compilers, in order to handle options such as `-DDEFINE=VALUE` or `-I/include/path`. This is the default parsing mode. New applications are recommended to use this mode.
3134
ParseAsCompactedShortOptions,
35+
/// `-abc` is interpreted as `--abc`, i.e. as the long option named `abc`. This is how Qt's own tools (uic, rcc...) have always been parsing arguments. This mode should be used for preserving compatibility in applications that were parsing arguments in such a way. There is an exception if the `a` option has the [`QCommandLineOption::ShortOptionStyle`] flag set, in which case it is still interpreted as `-a bc`.
3236
ParseAsLongOptions,
3337
}
3438

@@ -44,18 +48,24 @@ mod ffi {
4448
type QStringList = cxx_qt_lib::QStringList;
4549

4650
/// Adds help options to the command-line parser.
51+
///
52+
/// The options specified for this command-line are described by `-h` or `--help`. On Windows, the alternative `-?` is also supported. The option `--help-all` extends that to include generic Qt options, not defined by this command, in the output.
53+
///
54+
/// These options are handled automatically by `QCommandLineParser`.
4755
#[rust_name = "add_help_option"]
4856
fn addHelpOption(self: &mut QCommandLineParser) -> QCommandLineOption;
4957

50-
/// Adds the option option to look for while parsing.
51-
/// Returns true if adding the option was successful; otherwise returns false.
58+
/// Adds the option `option` to look for while parsing.
59+
/// Returns `true` if adding the option was successful; otherwise returns `false`.
60+
///
5261
/// Adding the option fails if there is no name attached to the option,
5362
/// or the option has a name that clashes with an option name added before.
5463
#[rust_name = "add_option"]
5564
fn addOption(self: &mut QCommandLineParser, option: &QCommandLineOption) -> bool;
5665

57-
/// Adds the -v / --version option, which displays the version string of the application.
58-
/// This option is handled automatically by QCommandLineParser.
66+
/// Adds the `-v` / `--version` option, which displays the version string of the application.
67+
///
68+
/// This option is handled automatically by `QCommandLineParser`.
5969
#[rust_name = "add_version_option"]
6070
fn addVersionOption(self: &mut QCommandLineParser) -> QCommandLineOption;
6171

@@ -67,7 +77,7 @@ mod ffi {
6777
#[rust_name = "clear_positional_arguments"]
6878
fn clearPositionalArguments(self: &mut QCommandLineParser);
6979

70-
/// Returns a translated error text for the user. This should only be called when parse() returns false.
80+
/// Returns a translated error text for the user. This should only be called when [`parse`](Self::parse) returns `false`.
7181
#[rust_name = "error_text"]
7282
fn errorText(self: &QCommandLineParser) -> QString;
7383

@@ -76,42 +86,75 @@ mod ffi {
7686
fn helpText(self: &QCommandLineParser) -> QString;
7787

7888
/// Returns a list of option names that were found.
89+
///
90+
/// This returns a list of all the recognized option names found by the parser, in the order in which they were found. For any long options that were in the form {–option=value}, the value part will have been dropped.
91+
///
92+
/// The names in this list do not include the preceding dash characters. Names may appear more than once in this list if they were encountered more than once by the parser.
93+
///
94+
/// Any entry in the list can be used with [`value`](Self::value) or with [`values`](Self::values) to get any relevant option values.
7995
#[rust_name = "option_names"]
8096
fn optionNames(self: &QCommandLineParser) -> QStringList;
8197

8298
/// Parses the command line arguments.
99+
///
100+
/// Most programs don't need to call this, a simple call to [`process`] is enough.
101+
///
102+
/// This function is more low-level, and only does the parsing. The application will have to take care of the error handling, using [`error_text`] if this function returns `false`. This can be useful for instance to show a graphical error message in graphical programs.
103+
///
104+
/// Calling this function instead of [`process`] can also be useful in order to ignore unknown options temporarily, because more option definitions will be provided later on (depending on one of the arguments), before calling [`process`].
105+
///
106+
/// Don't forget that arguments must start with the name of the executable (ignored, though).
107+
///
108+
/// Returns `false` in case of a parse error (unknown option or missing value); returns `true` otherwise.
109+
///
110+
/// [`process`]: Self::process
111+
/// [`error_text`]: Self::error_text
83112
fn parse(self: &mut QCommandLineParser, arguments: &QStringList) -> bool;
84113

85114
/// Returns a list of positional arguments.
115+
///
116+
/// These are all of the arguments that were not recognized as part of an option.
86117
#[rust_name = "positional_arguments"]
87118
fn positionalArguments(self: &QCommandLineParser) -> QStringList;
88119

89120
/// Processes the command line arguments.
121+
///
122+
/// In addition to parsing the options (like [`parse`](Self::parse)), this function also handles the builtin options and handles errors.
123+
///
124+
/// The builtin options are `--version` if [`add_version_option`](Self::add_version_option) was called and `--help` / `--help-all` if [`add_help_option`](Self::add_help_option) was called.
125+
///
126+
/// When invoking one of these options, or when an error happens (for instance an unknown option was passed), the current process will then stop, using the [exit](https://doc.qt.io/qt/qcoreapplication.html#exit)() function.
90127
fn process(self: &mut QCommandLineParser, arguments: &QStringList);
91128

92-
/// Sets the application description shown by helpText().
129+
/// Sets the application description shown by [`help_text`](Self::help_text).
93130
#[rust_name = "set_application_description"]
94131
fn setApplicationDescription(self: &mut QCommandLineParser, description: &QString);
95132

96-
/// Sets the parsing mode to parsingMode. This must be called before process() or parse().
133+
/// Sets the parsing mode to `single_dash_word_option_mode`. This must be called before [`process`](Self::process) or [`parse`](Self::parse).
97134
#[rust_name = "set_single_dash_word_option_mode"]
98135
fn setSingleDashWordOptionMode(
99136
self: &mut QCommandLineParser,
100-
singleDashWordOptionMode: QCommandLineParserSingleDashWordOptionMode,
137+
single_dash_word_option_mode: QCommandLineParserSingleDashWordOptionMode,
101138
);
102139

103-
/// Sets the parsing mode to parsingMode. This must be called before process() or parse().
140+
/// Sets the parsing mode to `parsing_mode`. This must be called before [`process`](Self::process) or [`parse`](Self::parse).
104141
#[rust_name = "set_options_after_positional_arguments_mode"]
105142
fn setOptionsAfterPositionalArgumentsMode(
106143
self: &mut QCommandLineParser,
107-
parsingMode: QCommandLineParserOptionsAfterPositionalArgumentsMode,
144+
parsing_mode: QCommandLineParserOptionsAfterPositionalArgumentsMode,
108145
);
109146

110-
/// Displays the version information from QCoreApplication::applicationVersion(), and exits the application.
147+
/// Displays the version information from [`QCoreApplication::application_version`](cxx_qt_lib::QCoreApplication::application_version), and exits the application.
148+
///
149+
/// This is automatically triggered by the `–version` option, but can also be used to display the version when not using [`process`](Self::process). The exit code is set to `EXIT_SUCCESS` (0).
111150
#[rust_name = "show_version"]
112151
fn showVersion(self: &mut QCommandLineParser);
113152

114153
/// Returns a list of unknown option names.
154+
///
155+
/// This list will include both long and short name options that were not recognized. For any long options that were in the form {–option=value}, the value part will have been dropped and only the long name is added.
156+
///
157+
/// The names in this list do not include the preceding dash characters. Names may appear more than once in this list if they were encountered more than once by the parser.
115158
#[rust_name = "unknown_option_names"]
116159
fn unknownOptionNames(self: &QCommandLineParser) -> QStringList;
117160
}
@@ -146,27 +189,44 @@ mod ffi {
146189
}
147190
}
148191

149-
/// QCoreApplication provides the command-line arguments as a simple list of strings.
150-
/// QCommandLineParser provides the ability to define a set of options, parse the command-line arguments, and store which options have actually been used, as well as option values.
192+
/// The QCommandLineParser class provides a means for handling the command line options.
193+
///
194+
/// Qt Documentation: [QCommandLineParser](https://doc.qt.io/qt/qcommandlineparser.html#details)
151195
#[derive(Debug, Clone)]
152196
#[repr(C)]
153197
pub struct QCommandLineParser {
154198
_space: MaybeUninit<usize>,
155199
}
156200

157201
impl QCommandLineParser {
158-
/// Returns the option value found for the given option name optionName, or an empty string if not found.
159-
pub fn value(&self, option_name: &ffi::QString) -> ffi::QString {
202+
/// Returns the option value found for the given option name `option_name`, or an empty string if not found.
203+
///
204+
/// The name provided can be any long or short name of any option that was added with [`add_option`](Self::add_option). All the option names are treated as being equivalent. If the name is not recognized or that option was not present, an empty string is returned.
205+
///
206+
/// For options found by the parser, the last value found for that option is returned. If the option wasn't specified on the command line, the default value is returned.
207+
///
208+
/// If the option does not take a value, a warning is printed, and an empty string is returned.
209+
pub fn value(&self, option_name: &QString) -> QString {
160210
ffi::qcommandlineparser_value(self, option_name)
161211
}
162212

163-
/// Returns a list of option values found for the given option name optionName, or an empty list if not found.
164-
pub fn values(&self, option_name: &ffi::QString) -> ffi::QStringList {
213+
/// Returns a list of option values found for the given option name `option_name`, or an empty list if not found.
214+
///
215+
/// The name provided can be any long or short name of any option that was added with [`add_option`](Self::add_option). All the options names are treated as being equivalent. If the name is not recognized or that option was not present, an empty list is returned.
216+
///
217+
/// For options found by the parser, the list will contain an entry for each time the option was encountered by the parser. If the option wasn't specified on the command line, the default values are returned.
218+
///
219+
/// An empty list is returned if the option does not take a value.
220+
pub fn values(&self, option_name: &QString) -> QStringList {
165221
ffi::qcommandlineparser_values(self, option_name)
166222
}
167223

168-
/// Checks whether the option name was passed to the application.
169-
pub fn is_set(&self, name: &ffi::QString) -> bool {
224+
/// Checks whether the option `name` was passed to the application.
225+
///
226+
/// Returns `true` if the option `name` was set, `false` otherwise.
227+
///
228+
/// The name provided can be any long or short name of any option that was added with [`add_option`](Self::add_option). All the options names are treated as being equivalent. If the name is not recognized or that option was not present, `false` is returned.
229+
pub fn is_set(&self, name: &QString) -> bool {
170230
ffi::is_set_from_qstring(self, name)
171231
}
172232
}

0 commit comments

Comments
 (0)