76 && settings.isSet(QQmlFormatSettings::s_indentWidthSetting)) {
77 setIndentWidth(settings.value(QQmlFormatSettings::s_indentWidthSetting).toInt()
);
80 if (!isMarked(
Settings::UseTabs) && settings.isSet(QQmlFormatSettings::s_useTabsSetting)) {
81 setTabsEnabled(settings.value(QQmlFormatSettings::s_useTabsSetting).toBool()
);
85 && settings.isSet(QQmlFormatSettings::s_maxColumnWidthSetting)) {
90 && settings.isSet(QQmlFormatSettings::s_normalizeSetting)) {
95 setNewline(QQmlFormatOptions::parseEndings(
96 settings.value(QQmlFormatSettings::s_newlineSetting).toString()));
100 && settings.isSet(QQmlFormatSettings::s_objectsSpacingSetting)) {
105 && settings.isSet(QQmlFormatSettings::s_functionsSpacingSetting)) {
110 && settings.isSet(QQmlFormatSettings::s_sortImportsSetting)) {
111 setSortImports(settings.value(QQmlFormatSettings::s_sortImportsSetting).toBool()
);
115 && settings.isSet(QQmlFormatSettings::s_singleLineEmptyObjectsSetting)) {
123 QCommandLineParser parser;
124 parser.setApplicationDescription(
125 "Formats QML files according to the QML Coding Conventions.\n"_L1
126 "Options below the \"Formatting options\" section can also be set via .qmlformat.ini"_L1
127 " unless --ignore-settings is used"_L1);
128 parser.addHelpOption();
129 parser.addVersionOption();
135 QCommandLineOption({
"V"_L1,
"verbose"_L1 },
136 QStringLiteral(
"Verbose mode. Outputs more detailed information.")));
138 QCommandLineOption writeDefaultsOption(
139 QStringList() <<
"write-defaults"_L1,
140 QLatin1String(
"Writes defaults settings to .qmlformat.ini and exits (Warning: This "
141 "will overwrite any existing settings and comments!)"_L1));
142 parser.addOption(writeDefaultsOption);
144 QCommandLineOption ignoreSettings(QStringList() <<
"ignore-settings"_L1,
145 QLatin1String(
"Ignores all settings files and only takes "
146 "command line options into consideration"_L1));
147 parser.addOption(ignoreSettings);
149 QCommandLineOption filesOption(
150 {
"F"_L1,
"files"_L1 },
"Format all files listed in file, in-place"_L1,
"file"_L1);
151 parser.addOption(filesOption);
154 QCommandLineOption dryrunOption(
155 QStringList() <<
"dry-run"_L1,
156 QStringLiteral(
"Prints the settings file that would be used for this instance."
157 "This is useful to see what settings would be used "
158 "without actually performing anything."));
159 parser.addOption(dryrunOption);
161 QCommandLineOption settingsOption(
162 {
"s"_L1,
"settings"_L1 },
163 QStringLiteral(
"Use the specified .qmlformat.ini file as the only configuration source."
164 "Overrides any per-directory configuration lookup."),
166 parser.addOption(settingsOption);
168 parser.addOption(QCommandLineOption(
169 {
"i"_L1,
"inplace"_L1 },
170 QStringLiteral(
"Edit file in-place instead of outputting to stdout.")));
174 parser.addOption(QCommandLineOption({
"f"_L1,
"force"_L1 },
176 "Continue even if an error has occurred.\n<><><><><><><><><>\nFormatting options\n<><><><><><><><><>"_L1
178 u"Continue even if an error has occurred.\n♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦\nFormatting options\n♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦"_s
186 parser.addOption(QCommandLineOption({
"t"_L1,
"tabs"_L1 },
187 QStringLiteral(
"Use tabs instead of spaces.")));
189 parser.addOption(QCommandLineOption({
"w"_L1,
"indent-width"_L1 },
190 QStringLiteral(
"How many spaces are used when indenting."),
191 "width"_L1,
"4"_L1));
193 QCommandLineOption columnWidthOption(
194 {
"W"_L1,
"column-width"_L1 },
195 QStringLiteral(
"Breaks the line into multiple lines if exceedes the specified width. "
196 "Use -1 to disable line wrapping. (default)"),
197 "width"_L1,
"-1"_L1);
198 parser.addOption(columnWidthOption);
199 parser.addOption(QCommandLineOption({
"n"_L1,
"normalize"_L1 },
200 QStringLiteral(
"Reorders the attributes of the objects "
201 "according to the QML Coding Guidelines.")));
204 parser.addOption(QCommandLineOption(
205 {
"l"_L1,
"newline"_L1 },
206 QStringLiteral(
"Override the new line format to use (native macos unix windows)."),
207 "newline"_L1,
"native"_L1));
209 parser.addOption(QCommandLineOption(
210 QStringList() <<
"objects-spacing"_L1,
211 QStringLiteral(
"Ensure spaces between objects (only works with normalize option).")));
213 parser.addOption(QCommandLineOption(
214 QStringList() <<
"functions-spacing"_L1,
215 QStringLiteral(
"Ensure spaces between functions (only works with normalize option).")));
218 QCommandLineOption({
"S"_L1,
"sort-imports"_L1 },
219 QStringLiteral(
"Sort imports alphabetically "
220 "(Warning: this might change semantics if a given "
221 "name identifies types in multiple modules!).")));
223 parser.addOption(QCommandLineOption(
224 QStringList() <<
"single-line-empty-objects"_L1,
225 QStringLiteral(
"Write empty objects on a single line (only works with normalize option).")));
227 QCommandLineOption semicolonRuleOption(
228 QStringList() <<
"semicolon-rule"_L1,
229 QStringLiteral(
"Specify the semicolon rule to use (always, essential).\n"
230 "always: always adds semicolon [default].\n"
231 "essential: adds only when ASI wouldn't be relied on."),
232 "rule"_L1,
"always"_L1);
233 parser.addOption(semicolonRuleOption);
235 parser.addPositionalArgument(
"filenames"_L1,
"files to be processed by qmlformat"_L1);
237 parser.process(args);
239 if (parser.isSet(writeDefaultsOption)) {
244 if (parser.positionalArguments().empty() && !parser.isSet(filesOption)) {
245 options.addError(
"Error: Expected at least one input file."_L1);
249 bool indentWidthOkay =
false;
250 const int indentWidth = parser.value(
"indent-width"_L1).toInt(&indentWidthOkay);
251 if (!indentWidthOkay) {
252 options.addError(
"Error: Invalid value passed to -w"_L1);
257 if (!parser.value(
"files"_L1).isEmpty()) {
258 const QString path = parser.value(
"files"_L1);
260 if (!file.open(QIODevice::Text | QIODevice::ReadOnly)) {
261 options.addError(
"Error: Could not open file \""_L1 + path +
"\" for option -F."_L1);
265 QTextStream in(&file);
266 while (!in.atEnd()) {
267 QString file = in.readLine();
272 files.push_back(file);
275 if (files.isEmpty()) {
276 options.addError(
"Error: File \""_L1 + path +
"\" for option -F is empty."_L1);
280 for (
const auto &file : std::as_const(files)) {
281 if (!QFile::exists(file)) {
282 options.addError(
"Error: Entry \""_L1 + file +
"\" of file \""_L1 + path
283 +
"\" passed to option -F could not be found."_L1);
288 const auto &args = parser.positionalArguments();
289 for (
const auto &file : args) {
290 if (!QFile::exists(file)) {
291 options.addError(
"Error: Could not find file \""_L1 + file +
"\"."_L1);
298 options.setIsVerbose(parser.isSet(
"verbose"_L1));
299 options.setIsInplace(parser.isSet(
"inplace"_L1));
300 options.setForceEnabled(parser.isSet(
"force"_L1));
301 options.setIgnoreSettingsEnabled(parser.isSet(
"ignore-settings"_L1));
303 if (parser.isSet(
"tabs"_L1)) {
307 if (parser.isSet(
"normalize"_L1)) {
311 if (parser.isSet(
"objects-spacing"_L1)) {
315 if (parser.isSet(
"functions-spacing"_L1)) {
319 if (parser.isSet(
"sort-imports"_L1)) {
323 if (parser.isSet(
"single-line-empty-objects"_L1)) {
327 if (parser.isSet(
"indent-width"_L1)) {
332 if (parser.isSet(
"newline"_L1)) {
334 options.setNewline(QQmlFormatOptions::parseEndings(parser.value(
"newline"_L1)));
337 if (parser.isSet(settingsOption)) {
339 const auto value = parser.value(settingsOption);
340 if (value.isEmpty()) {
341 options.addError(
"Error: No settings file specified for option -s."_L1);
344 if (!QFile::exists(value)) {
345 options.addError(
"Error: Could not find file \""_L1 + value +
"\"."_L1);
348 options.setSettingsFile(value);
351 if (parser.isSet(semicolonRuleOption)) {
353 const auto value = parser.value(semicolonRuleOption);
354 if (value ==
"always"_L1) {
355 options.setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule::Always);
356 }
else if (value ==
"essential"_L1) {
357 options.setSemicolonRule(QQmlJS::Dom::LineWriterOptions::SemicolonRule::Essential);
359 options.addError(
"Error: Invalid value passed to --semicolon-rule."_L1);
363 options.setFiles(files);
364 options.setArguments(parser.positionalArguments());
366 if (parser.isSet(columnWidthOption)) {
367 bool isValidValue =
false;
368 const int maxColumnWidth = parser.value(columnWidthOption).toInt(&isValidValue);
369 if (!isValidValue || maxColumnWidth < -1) {
370 options.addError(
"Error: Invalid value passed to -W. Must be an integer >= -1"_L1);