nushell.github.io icon indicating copy to clipboard operation
nushell.github.io copied to clipboard

Document arguments to autocomplete commands

Open bgmort opened this issue 2 years ago • 0 comments

The documentation on completions has no documentations or examples on completions that the arguments passed to autocomplete commands for providing textual completion, or even suggest that they exist. In order to write a completer that makes use of them, I first had to experiment to find out what they were. An example that uses the arguments to produce the completion list would be helpful. Here's a possible example. I'm happy to submit a PR for it if it's useful.

let places = {
	usa: [ut, ca, co],
	canada: [bc, on, qc],
}

def country-autocomplete [args arglength] {
	$places | columns
}

def region-autocomplete [args arglength] {
	let parts = ($args | split words | skip 1)
	try {
		let country = $parts.0
		let result = ($places | get $country)
		$result
	} catch {
		let result = ($places | columns)
		$result
	}
}

# get the weather for a given country and region
export def "weather" [
	country: string@"country-autocomplete" # country name
	region: string@"region-autocomplete" # region code
] {
	print $"getting weather for ($region), ($country)"
}

bgmort avatar Feb 17 '23 23:02 bgmort