Commit Graph
419 Commits
Author SHA1 Message Date
Nate Cook af1be50a93 Run the StandardizeDocumentationComments rule 2025-03-07 12:15:56 -06:00
Rauhul Varma d67151befa Remove resolved bug from CI workflow (#747) 2025-03-06 08:59:22 -08:00
Nate Cook 134451f572 Fix up license headers and enable check (#746) 2025-02-24 06:53:19 -08:00
Nate Cook ef4f15f854 Improve output when validation fails (#744)
With backtrace support built into command-line Swift, calling `fatalError`
for configuration/validation errors isn't very effective, since the error
messages get hidden by the backtrace. This switches to simply printing
the validation message to stderr and exiting with a failing error code,
which possibly should have always been the behavior.

This also revamps the file structure for validators.
2025-02-23 08:30:51 -06:00
Nate Cook be711ca2fc Fix several one-line summary doc comment issues (#743) 2025-02-22 12:29:24 -08:00
Nate Cook 5bb54b937c Specify availability for remainder of platforms (#741) 2025-02-16 21:08:42 -06:00
Ross Goldberg bdb3b27a68 Improve bash completion script generation (#735)
* Restrict access to symbols in BashCompletionsGenerator.swift.

* Use key path instead of closure in BashCompletionsGenerator.swift.

* Remove extraneous bash spacing.

* Do not indent bash cases.

Standardize bash indents.

* Do not prefix bash cases with an open parenthesis.

* Brace & quote bash variable uses.

* Remove extraneous bash blank line.

* Improve bash escaping.

* Improve bash $cur, $prev, & $COMPREPLY.

Use positional arguments passed by bash to the main completion function instead of reading from COMP_WORDS, as that can return the wrong info if completing an empty word before a non-empty word.

Make $cur & $prev local & readonly.

Remove unnecessary COMPREPLY=().

* Improve bash $SAP_SHELL & $SAP_SHELL_VERSION.

Make them local & readonly.

* Overhaul BashCompletionsGenerator.swift as [ParsableCommand.Type] extension.

Inline some single-use functions.

* Move bashValueCompletion(…) in BashCompletionsGenerator.swift.

Move from ArgumentDefinition extension to [ParsableCommand.Type] extension.

* Overhaul bash completion script generation:

Attempt to emulate the much more complete & correct zsh completion script.

Offer candidates for only the current positional / option value, not for all.

Generate completions for positionals the same as for option values.

Offer flags & options only if no prior option-terminator marker (a standalone --).

Offer flags & options only if current word starts with a -, or if there are no remaining positional parameters.

Parse options prior to subcommands later in the command line.

Offer flags & options only once.

Do not offer flags or options if the current word is an option value.

* Add default help to bash completions iff no existing help subcommand.

* Improve bash file & directory completions.

Do not split paths with spaces into separate completions.

Escape spaces in paths.

Append / to directory paths.

Do not use _filedir from bash-completions; use builtin bash constructs instead.

Fix broken existing escaping of single quotes in file extension filters.

More succinct & performant.

* Disable history ! in bash completion scripts.

* Do not include uppercased extensions in bash file(extensions:) completions.

That behavior was a bug, not a feature.

Released Swift Argument Parser documentation says:

"Complete file names with the specified extensions."

It does not mention including uppercase versions of the extensions.

None of the other shells include uppercase versions of extensions.

Why should uppercase versions be special? Why not case-insensitive matching? Why not lowercase versions? Etc.

Any config depending on this behavior won't match uppercase extensions without manually being reconfigured, but there are a ton of other bug fixes that can also break compatibility.

* Use single quoted string for bash list completions.

bash scripts now escape single quotes in list values.

Any existing list values with escapes that worked in double quotes will not work in the single quotes. But now:

- list values needn't be escaped
- unescaped double quotes in list values won't break the script
- $ & other characters won't interact with the shell, so, e.g., command substitutions cannot cause problems

* Allow bash list completions to contain spaces.

* Allow bash custom completions to contain spaces.

Properly refuses to complete when there are no completion candidates.

Still cannot complete to an empty string.

* bash custom completion of empty word followed by other words.

* Prevent bash shellCommand completion scripting from breaking scripts.

Eval the given command from a single-quoted string instead of running it directly in the shell.

* Allow bash shellCommand completions to contain spaces.

If existing shellCommand completions depend on spaces as completion delimiters, they will not work anymore. Newlines are now the only supported delimiters.

Resolve #734

* Use zip(…) to generate bash positional argument numbers.

* Rework bash positional case generation.

* Reverse the polarity of the neutron flow of the completions help subcommand detector.

* Rename flags & options vars to flagCompletions & optionCompletions, respectively, in BashCompletionsGenerator.swift.

* Remove TODO comments from generated bash completion scripts.

---------

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-02-16 13:06:01 -06:00
Nate Cook 74f1f8c939 Simplify configuration check for async root command (#736)
If the synchronous `main()` is running for an `AsyncParsableCommand`
root, something went wrong. Previously, this was only diagnosed when
there was an async subcommand; this change expands the failure
condition to include standalone async commands.

Fixes #662.
2025-02-15 12:25:30 -08:00
Ross Goldberg f2eda39df5 Improve zsh completion script generation (#727)
* Do not indent zsh cases.

Simplify zsh indent generation.

* Do not prefix zsh cases with an open parenthesis.

* Prevent zsh parameter word splitting.

Brace & quote parameter uses.

Use [@] for quoted array output.

* Improve comment in ZshCompletionsGenerator.swift.

* Fix incorrect zsh shellCommand single quotes:

4 consecutive single quotes were obviously intended to be 2 escaped single quotes, 
but that isn't zsh syntax.

Use 2 double quotes instead.

* Remove extraneous zsh newline.

* Improve zsh variable declarations: scoping, typing & readonly.

Remove trailing spaces from InstallingCompletionScripts.md.

* Include zsh words before current subcommand in custom completion arg.

* Make subcommandHandler in ZshCompletionsGenerator.swift immutable.

* Escape zsh single quotes via '\'' instead of via '"'"'.

* Escape single quotes in zsh shellCommand String.

If someone already escapes single quotes from the String, this will cause an issue, 
but no one should be required to go to the trouble to manually escape single quotes 
in their script, especially since the requirement isn't documented or normal.

* Fix zsh custom completions for empty [String] & String elements.

If a Swift custom completion function returns an empty [String], if the user tries to 
complete it, refuse to complete instead of inserting a blank space into the command line.

If a Swift custom completion function returns a [String] including a Swift empty String 
or including a String with a description but with a blank completion 
(e.g., ":description"), if that completion is selected, complete to a zsh empty string 
'' instead of inserting a blank space into the command line.

Disambiguating between an empty [String] & a [String] with one empty String element 
requires that an extra value be appended to the output of the Swift custom function, 
which is then removed by the completion script.

* Simplify zsh subcommand completion function dispatch.

* Restrict access to symbols in ZshCompletionsGenerator.swift.

* Add default help to zsh completions iff no existing help subcommand.

* Use interpolated Strings in ZshCompletionsGenerator.swift.

* Create & use zsh __completion function.

* Improve zsh escaping.

* Set zsh settings to a known state.

Disable history ! in zsh completion scripts.

* Inline single-use functions & variables in ZshCompletionsGenerator.swift.

* Overhaul ZshCompletionsGenerator.swift as [ParsableCommand.Type] extension.

* Move functions in ZshCompletionsGenerator.swift.

Move from ArgumentDefinition extension to [ParsableCommand.Type] extension.

* Move zsh helper functions before command functions to mirror other shells.

* Prefix zsh helper functions with command name to prevent naming clashes.

Function names are globally scoped.

Without namespacing, if 2 programs use different versions of Swift Argument Parser,
one could overwrite the other's different version of the same helper function.

Renamed functions from *_completion to *_complete, as they complete, not return a 
completion.

* Separate zsh _arguments flags from specs using :.

* Rename zsh args variable as arg_specs.

* Simplify zshCompletionString(…).

* Allow generating zsh setup scripts for arguments.

* Use zsh array for list completions instead of nested strings.

Allows list completions to contain spaces.

Resolve #726

* Make CompletionShell.format(…) internal instead of public.

* Reword uses of "iff" in completions code.

Redid a comment as a DocC.

* Replace zsh END_MARKER pseudo-completion with a space to ease migration.

Document why & how this pseudo-completion is used.

Do not trim whitespace in testing, as that breaks with the space pseudo-completion.

Testing should be as exact as possible; trimming whitespace makes it less exact.

* Throw error if attempting to generate a zsh completion script for no commands.

Force unwrap first in ZshCompletionsGenerator.swift.

---------

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-02-15 10:56:19 -08:00
Nate Cook 385cfb2ad9 Add script to run the correct invocations of swift format (#730) 2025-02-14 10:18:55 -08:00
Nate Cook 7692b6280a Add CI job with cmake build (but not tests yet) (#732) 2025-02-14 12:15:52 -06:00
Dong-Sig Han 35fe082c5d Update CMakeLists.txt: added Mutex.swift (#731)
Recently, Utilities/Mutex.swift was created but omitted in CMakeLists.txt
2025-02-14 10:16:12 -06:00
Nate Cook 6cf94c7d8e Drop availability checks for macOS 10.13 (#729)
As @rgoldberg points out, the bump to Swift 5.7 makes these
availability checks unnecessary.

Fixes #725.
2025-02-13 15:18:59 -08:00
Nate Cook 56d4248a63 Fix confusing error message w/ single-dash option (#728)
When a required option has a short name, longer single-dash arguments
can end up incorrectly colliding and causing confusing error messages.
This changes the error handling when a short-name option is missing
a value to show both the missing-value error _and_ a message about
the unrecognized longer/composite option.

Fixes #709.
2025-02-13 17:14:42 -06:00
Ross Goldberg 5b1320e9d6 Make CommandParser.swift import Swift version consistent with other files. (#724) 2025-02-12 19:19:40 -08:00
Rauhul Varma fd8ea096da Remove @_implementationOnly imports (#666)
The `@_implementationOnly` attribute does not work correctly without
library evolution enabled. The only possible client with this enabled is
Apple, however they have updated to the Swift 6 compiler and can use
`internal import` instead of this attribute. Therefore there is no need
for this package to use the attribute anymore.
2025-02-12 13:01:44 -08:00
Ross Goldberg 23d8d3a522 Prevent implementation-only import build warnings on older Swift. (#720)
Resolve #719

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-02-12 11:00:23 -06:00
Rauhul Varma 47fe00c4fe Enable more ci (#712)
Enables and fixes issues with additional platform tests 
and the formatter with these additional rules:

- UseLetInEveryBoundCaseVariable
- NeverForceUnwrap
- BeginDocumentationCommentWithOneLineSummary
- ValidateDocumentationComments
- AlwaysUseCamelCase
2025-02-12 08:52:17 -06:00
Ross Goldberg 462737a8f1 Fix swift-format violations. (#714)
Resolve #713

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-02-11 16:44:28 -08:00
Ross Goldberg 201b569895 Remove unused completion script test snapshots. (#716)
Resolve #715

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-02-11 16:28:23 -08:00
Ross Goldberg ddb64330a8 Prevent unhandled file build warnings. (#718)
Resolve #717

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-02-11 16:25:21 -08:00
Rauhul Varma 10d80282e5 Enable swift-format checking (#711)
Adopts the common swift-mmio and swift-argument-parser format and enables CI checking.

Fixes: #702
2025-02-10 14:58:19 -08:00
Rauhul Varma a01856b49f Enable Documentation check (#708)
Enables the documentation check from the swiftlang soundness workflow
and resolves errors/notes emitted by the check.

Fixes: #704
2025-02-07 22:02:25 -06:00
Rauhul Varma 33acc79c36 Add soundness workflows (#701)
Adds CI for the following checks:
- YAML lint
- Python lint
- Unacceptable language
- Broken symlinks
- API breakages

In the future the following checks will be added:
- Documentation
- License headers
- Formatting
- Shell check
2025-02-07 11:24:00 -08:00
Ross Goldberg aefbb6e28d Fix Swift 5.7 build errors. (#707) 2025-02-06 20:28:38 -08:00
Rauhul Varma 1248cc4bda Fix concurrency warnings (#705)
Moves static read-only vars behind a mutex so they are currency safe.
Users of the api don't need to worry about this because the mutex is
hidden behind an accessor.
2025-02-06 15:02:53 -08:00
Rauhul Varma d3630e3190 Move additional tests to snapshots (#700)
Moves some examples test to use snapshot files instead of inline
multiline strings.
2025-02-06 10:28:22 -08:00
Rauhul Varma 45b5c745c7 Combine completion script tests (#699)
Removes a ton of redundant completion script tests and combines unique
components into a single common set of tests.
2025-02-06 09:50:30 -08:00
Rauhul Varma cea1a0837e Extend snapshot testing to completion scripts (#698)
As the title suggests this commit moves the completion script tests to
use the snapshot testing recently introduced.
2025-02-06 09:40:18 -08:00
Rauhul Varma b551ec8c04 Add completion kind and help command to ToolInfo (#697)
Updates ToolInfo to include the built-in `help` subcommand and also
include details on each argument's completion method if any.
2025-02-06 08:55:56 -08:00
Rauhul Varma c2d8414653 Refactor dump help tests (#696)
The dump help tests previously included the json text in the Swift
source files as multiline strings. This made updating them very tedious
and made diffs hard to follow. This commit moves each of the json dumps
into their own files and adds an easy way of recording new ones as
needed.
2025-02-05 20:25:05 -08:00
Rauhul Varma 9289e315f8 Add generate-docc-reference plugin (#694)
Upstreams swiftly's generate-docs-plugin with minimal changes. Adds
tests against argument-parser's built in example tools.

The initial version of this tool is extremely minimal and should be
extended to output much more information contained in tool info, like
generate-manual does.
2025-02-05 10:05:45 -08:00
Rauhul Varma 25db036b71 Refactor serialization of dump help descriptions (#693)
Cleans up some lingering odds and ends from #647. Specifically switches
CommandInfoV0 to use the old style description and updates
ArgumentInfoV0 to emit value descriptions as a separate dictionary.
2025-02-04 17:04:42 -08:00
Rauhul Varma 1fdca83051 Add tests parsing dump help output (#692)
Adds a new test target called ArgumentParserToolInfoTests which contains
example json output from dump-help. The test code attempts to parse all
json files in the 'Examples' subdirectory. This will help us catch cases
where ArgumentParser is unable to parse json generated by older versions.

We still need to add a test that asserts old ArgumentParser versions can
parse the output of new versions.
2025-02-04 14:53:18 -08:00
Fabrice de Gans 27e280f69e [cmake] Install libraries in standard directories (#685)
* [cmake] Install libraries in standard directories

Previously, libraries were installed under `lib/swift/${os}/`. They
should be installed in the default library directory for the relevant
target system.
In addition, swiftmodules were installed in the older layout format on
non-Darwin platforms. This changes to use the standard modern layout
format for swiftmodules.

* Use full triple for swiftmodule filenames

* Extract Swift_MODULE_TRIPLE from the function

* Fail early in case the triple cannot be found

* Enable C language before using GNUInstallDirs

* Add C language in project definition

Enabling the C language after Swift was causing issues with using the C
compiler.
2025-02-03 09:13:09 -08:00
Ross Goldberg 5f48a1614f Add singleton to indicate which shell version is requesting completion candidates (#690)
Create a `String?` singleton named `CompletionShell.requestingVersion` that
indicates which shell version is requesting completion candidates.

It will be set to the correct value while a Swift custom completion function is
executing to offer completions for a word from a command line (e.g., while
`customCompletion` from `@Option(completion: .custom(customCompletion))`
executes). Otherwise, it will be set to `nil`.

The requesting shell version is communicated to the Swift app via an environment
variable named `SAP_SHELL_VERSION`, which is exported by each of the generated
completion scripts.

Improve some nearby DocC.

Resolve #689

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-01-22 09:22:35 -06:00
Ross Goldberg 2edadce25d Add singleton to indicate which shell is requesting completion candidates (#680)
A CompletionShell singleton named CompletionShell.requesting has been created
that indicates which shell is requesting completion candidates.

The singleton is populated when a completion script is generated, so functions
used to generate arguments for CompletionKind creation functions can return
completion candidate syntax / shell commands tailored for that shell.

For the custom(:) CompletionKind creation function, the singleton is populated
at runtime (when a completion script requests completions from the Swift app
after a user types tab while composing a command line to call the app).

The requesting shell is communicated to the Swift app via an environment variable
named SAP_SHELL, which is exported by each of the generated completion scripts.

Resolve #672

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2025-01-21 23:57:36 -06:00
Ross Goldberg 41d58ffe70 Fix incorrect or confusing documentation about Swift versions (#678)
Rewrote Markdown to properly specify Swift 5.7 instead of 5.5,
and to clarify other information related to Swift versions.

Resolve #677

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2024-12-04 17:44:38 -08:00
Saleem Abdulrasool d6836f4508 build: repair the CMake based build (#684)
Add the missing source file to the package to allow building with CMake
again.
2024-11-19 16:19:04 -08:00
Ross Goldberg e4009c1616 Fix Swift 5.7 build errors (#676)
Insert missing comma in targets array in Package.swift to fix Swift 5.7 build
break from commit 83c5134a24.

Replace 2 if expressions (which are not supported by Swift 5.7) that were added
to ToolInfo.swift by commit 83c5134a24.

Replace switch expression (which is not supported by Swift 5.7) that was added
to GenerateManual.swift by commit 44dd206a85.

Replace switch expression (which is not supported by Swift 5.7) that was added
to DumpHelpGenerationTests.swift by commit 83c5134a24.

Fixes #675

Signed-off-by: Ross Goldberg <484615+rgoldberg@users.noreply.github.com>
2024-11-06 08:57:16 -08:00
Joseph Heck 72bf21248f extend ToolInfoV0 with command visibility (#669)
- resolves https://github.com/apple/swift-argument-parser/issues/668
  by extending the ToolInfoV0 (help dump) struct to include visibility
  information for commands (already exists for arguments).
- updates test output to verify existing examples extend with the
  additional key.
2024-10-07 13:03:07 -05:00
Bri Peticca 83c5134a24 Add ability to provide descriptions for CaseEnumerable @Option values (#647)
Since `ExpressibleByArgument` already maintains a list of
enumerable values for an argument, we can extend this to serve
as an ordered list for a new dictionary property that maps the
value name to its description, if applicable. The new property
is a static variable on `ExpressibleByArgument` labelled
`allValueDescriptions`.

If the description string for a value is the same as the value
string, it's assumed that the description is not implemented.

The new value strings are used in the help screen, in the 
dump-help JSON output, and in the generated manual.
2024-09-30 14:17:48 -05:00
Joseph Heck 7f9f965856 removes hidden arguments for generate-manual (#667)
- adds check in single and multipage to leverage hidden description in
  help dump to determine if the argument should be displayed
- adds check in argument synopsis to only display synopsis if not hidden
- updates existing tests that _had_ hidden output displayed in the
  generate-manual output
2024-09-30 13:46:45 -05:00
Rauhul Varma 44dd206a85 Improve generate-manual error descriptions (#663)
Adds a human readable description to `GenerateManualError` and updates
the non-zero exit code error to include stderr to aid debugging.

Fixes: #653
2024-09-14 18:58:36 -07:00
Christian GoetzeandChristian Goetze e3d8a3dd42 Add missing @Flag initializer in example (#657)
The documentation as presented doesn't compile. An initializer is
required for a boolean flag.

rdar://132579907

Co-authored-by: Christian Goetze <cgoetze@apple.com>
2024-07-29 13:55:30 -07:00
Nate Cook 41982a3656 Update CHANGELOG for the 1.5 release. (#654) 1.5.0 2024-07-18 10:46:38 -07:00
finagolfin 4a2e245ad5 Import new Android overlay (#651) 2024-07-16 16:38:41 -05:00
Nate Cook bb10ca8b70 Updates to support Swift 6 language version (#650)
Some minor NFC changes to resolve strict concurrency checking
warnings/errors and the warning about the change in meaning for
`#file`.
2024-07-15 15:37:38 -07:00
Neil Jones 6a7abc050b add support for riscv64 (#649) 2024-07-05 12:17:14 -07:00
Craig Siemens d66d015418 Fix zsh/bash completions for arguments in option groups with a custom completion (#648)
* Added conversion for InputKey to/from fullPathString.

* Updated custom completions to use the IndexKey.fullPathString.

This resolves an issue where custom completion for arguments in an OptionGroup would fail to match the argument. It was caused by:
- the completion script only using the name of the argument (instead of the full path)
- the CommandParser looking for the matching argument by comparing a name only IndexKey with the “full” IndexKeys

* Updated BashCompletionsGenerator to use customCompletionCall.

The zsh completions already uses this function. The function’s implementation is the same as what the BashCompletionsGenerator is doing. This removes the duplicated logic.

* Updated completion tests to include nested arguments with custom completions.

* Switched to using the split method from the stdlib.

Prevously was using .components(seperatedBy:) from Foundation.

* Updated the fish completions to include arguments
2024-07-04 00:52:58 -05:00