13 Commits
Author SHA1 Message Date
Rauhul Varma 6924db870d Use Foundation Essentials when possible (#804)
* add implementation of replacing(_:with:)
* remove use of range(of:)
* Consolidate foundation usage
* Consolidate error to string logic
* Clean up env a bit
2025-09-11 18:44:31 -05:00
Allan Shortlidge 4f680a7e9c Adopt upcoming Swift language feature MemberImportVisibility (#803)
* Adopt upcoming Swift language feature MemberImportVisibility.

Enable the language feature flag for SE-0444. This instructs the compiler to
be stricter about requiring import declarations in order to access member
declarations.

* Replace `#if swift(>=6.0)` guards with `#if compiler(>=6.0)`.

All uses of `internal import` were previously guarded with `#if swift(>=6.0)`,
presumably to handle differences in compiler support for `internal import`.
However, `#if swift(>=)` is not the correct directive to use for this purpose,
as it primarily tests the language mode that is enabled. These existing
conditionals never evaluated true when building this package since it is not
configured to build with Swift 6 enabled. Instead, use `#if compiler(>=6.0)`
which correctly predicates the imports on only the compiler version.
2025-09-08 11:05:36 -07:00
Nate Cook 134451f572 Fix up license headers and enable check (#746) 2025-02-24 06:53:19 -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
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
Gwynne Raskind c10af98655 Respect the COLUMNS and LINES environment variables when present (#596)
* Respect the `COLUMNS` and `LINES` environment variables, if set, when determining screen size.
* Add test for COLUMNS environment override
* Make columns test idempotent against there being a COLUMNS value already set in the environment
* Make help tests be more explicit about screen widths.
2023-11-14 21:17:06 -08:00
Nate Cook 1141ed1e1b Support an async entry point for commands (#404)
Adds a new `AsyncParsableCommand` protocol, which provides a
`static func main() async` entry point and can call through to the root
command's or a subcommand's asynchronous `run()` method. For this
asynchronous execution, the root command must conform to `AsyncParsableCommand`,
but its subcommands can be a mix of asynchronous and synchronous commands.

Due to an issue in Swift 5.5, you can only use `@main` on an
`AsyncParsableCommand` root command starting in Swift 5.6.
This change also includes a workaround for clients that are using Swift 5.5.
Declare a separate type that conforms to `AsyncMainProtocol` and add the `@main`
attribute to that type.

```
@main enum Main: AsyncMain {
    typealias Command = <#command#>
}
```
2022-03-14 18:14:09 -05:00
Jake Petroules b2e411887e Fix compile failure on iOS for Mac Catalyst support (#372) 2021-11-08 19:50:43 -06:00
Gonzalo RH 530a754555 Included help message when a required value is missing. (#324) 2021-06-09 11:28:00 -05:00
Alfredo Delli Bovi 4793b0f4b9 Include help text in error message when validation fails (#283) 2021-03-06 13:11:58 -06:00
Nate Cook 85196ee1d8 Additional help messages (#165)
* Add 'see help' messages to usage messages and the help screen

* Update tests for new help messages.

* Update guide examples with additional help messages
2020-05-22 17:04:14 -05:00
Nate Cook ddc828f8cb Add an API for converting an error to an exit code (#79)
* Add an API for converting an error to an exit code

* Make ExitCode more useful as a value type

* Update tests to use ExitCode values

* Typo fix

* Add a test for ExitCode.isSuccess

* Switch to just using ExitCode for tests
2020-03-21 12:44:32 -05:00
Elliott Williams 34300696f5 Prefix test target names with "ArgumentParser" (#74)
* Prefix testing and test helper targets with ArgumentParser

* Replace SAP with ArgumentParser in imports and CMakeLists
2020-03-10 12:44:07 -05:00