text stringlengths 0 15.7k | source stringlengths 6 112 |
|---|---|
return (n as text) & item (n mod 10) of {"st", "nd", "rd"} | lns3.scpt |
return (n as text) & "th" | lns3.scpt |
end _ordinalNumber | lns3.scpt |
to _normalizeTrailingSpace(theText) -- append space to text only if necessary | lns3.scpt |
ignoring white space -- (hyphens, punctuation must already be considered) | lns3.scpt |
if theText's length > 0 and theText's character -1 is not "" then set theText to theText & space | lns3.scpt |
end _normalizeTrailingSpace | lns3.scpt |
to _parseArguments(argumentsList, argumentDefinitions, asocParametersDict) | lns3.scpt |
set argCount to argumentsList's length | lns3.scpt |
set mustBeOptional to false -- repeat loop will throw invalid argument definition error if an optional argument definition is followed by a required argument definition | lns3.scpt |
repeat with argRef in argumentDefinitions | lns3.scpt |
set argumentDefinition to (argRef's contents as record) & {propertyName:"", valueType:text, isList:false, defaultValue:NoValue, valuePlaceholder:""} | lns3.scpt |
if argumentDefinition's defaultValue is not NoValue then | lns3.scpt |
set mustBeOptional to true | lns3.scpt |
else if mustBeOptional and argumentDefinition's defaultValue is NoValue then | lns3.scpt |
error "Invalid argument definition (a non-optional argument cannot follow an optional argument)." number -1703 from argRef | lns3.scpt |
if argumentDefinition's propertyName is "" then error "Invalid argument definition (record must contain a non-empty propertyName property)." number -1703 from argRef | lns3.scpt |
if argumentsList = {} then | lns3.scpt |
set theValue to argumentDefinition's defaultValue | lns3.scpt |
if theValue is NoValue then -- record doesn't have a defaultValue property, so user should've supplied argument | lns3.scpt |
set placeholderText to argumentDefinition's valuePlaceholder | lns3.scpt |
if placeholderText's length = 0 then set placeholderText to _defaultValuePlaceholder(argumentDefinition) | lns3.scpt |
error "Missing " & _ordinalNumber(i) & " required argument (expected " & placeholderText & ")." number _ArgvUserError | lns3.scpt |
set theValue to _unpackValue(first item of argumentsList, argumentDefinition) | lns3.scpt |
set argumentsList to rest of argumentsList | lns3.scpt |
if argumentDefinition's isList then | lns3.scpt |
if i < length of argumentDefinitions then error "Invalid argument definition (only the last argument definition may contain an ‘isList:true’ property)." number -1703 from argRef | lns3.scpt |
set theValue to {theValue} | lns3.scpt |
repeat with aRef in argumentsList | lns3.scpt |
set end of theValue to _unpackValue(aRef's contents, argumentDefinition) | lns3.scpt |
set argumentsList to {} | lns3.scpt |
(asocParametersDict's setObject:theValue forKey:(argumentDefinition's propertyName)) | lns3.scpt |
if argumentsList is not {} then error "Too many arguments (expected " & length of argumentDefinitions & " but received " & argCount & ")." number _ArgvUserError | lns3.scpt |
end _parseArguments | lns3.scpt |
to _formatOptions(optionDefinitions, vtStyle, hasDefaultOptions) | lns3.scpt |
if optionDefinitions is {} and not hasDefaultOptions then return {"", ""} | lns3.scpt |
set {defaultOptions, booleanOptions, otherOptions} to {"", "", ""} | lns3.scpt |
set optionsSection to vtStyle's b & "OPTIONS" & vtStyle's n | lns3.scpt |
set optionDefinition to (optionRef as record) & {shortName:"", longName:"", valueType:text, isList:false, defaultValue:NoValue, valuePlaceholder:"", valueDescription:""} | lns3.scpt |
set shortName to optionDefinition's shortName as text | lns3.scpt |
set longName to optionDefinition's longName as text | lns3.scpt |
set valueType to _asValueType(optionDefinition's valueType) | lns3.scpt |
set isList to optionDefinition's isList as boolean | lns3.scpt |
set valuePlaceholder to optionDefinition's valuePlaceholder as text | lns3.scpt |
set valueDescription to _normalizeTrailingSpace(optionDefinition's valueDescription as text) | lns3.scpt |
_support's throwInvalidParameter(optionDefinitions, "options", list, "not a valid list of ‘command line option definition’ records: " & eText) | lns3.scpt |
set optionsSection to optionsSection & LF2 & Indent1 | lns3.scpt |
if shortName is "" then | lns3.scpt |
if longName is "" then error "Invalid option definition (record must contain a ‘shortName’ and/or ‘longName’ property)." number -1703 from optionRef | lns3.scpt |
set optionName to longName | lns3.scpt |
set optionsSection to optionsSection & "--" & longName | lns3.scpt |
set optionName to shortName | lns3.scpt |
set optionsSection to optionsSection & "-" & shortName | lns3.scpt |
if longName is not "" then set optionsSection to optionsSection & ", --" & longName | lns3.scpt |
if valueType is boolean then -- group all boolean flags as "[-N…]" | lns3.scpt |
set booleanOptions to booleanOptions & optionName | lns3.scpt |
set isOptional to optionDefinition's defaultValue is not NoValue | lns3.scpt |
set otherOptions to otherOptions & space | lns3.scpt |
if isOptional then set otherOptions to otherOptions & "[" | lns3.scpt |
set otherOptions to otherOptions & "-" & optionName | lns3.scpt |
set valuePlaceholder to valuePlaceholder | lns3.scpt |
if valuePlaceholder is "" then set valuePlaceholder to _defaultValuePlaceholder(optionDefinition) | lns3.scpt |
set valuePlaceholder to vtStyle's u & valuePlaceholder & vtStyle's n | lns3.scpt |
set otherOptions to otherOptions & space & valuePlaceholder | lns3.scpt |
if isOptional then set otherOptions to otherOptions & "]" | lns3.scpt |
set optionsSection to optionsSection & space & valuePlaceholder | lns3.scpt |
if valueType's class is list then set valueDescription to valueDescription & _formatEnumeration(valueType) | lns3.scpt |
if optionDefinition's defaultValue is not NoValue then set valueDescription to valueDescription & _formatDefaultValue(optionDefinition) | lns3.scpt |
if isList then set valueDescription to valueDescription & "This option can appear multiple times. " | lns3.scpt |
if valueDescription is not "" then set optionsSection to optionsSection & linefeed & Indent2 & valueDescription | lns3.scpt |
if booleanOptions does not contain "h" then | lns3.scpt |
set defaultOptions to "h" & defaultOptions | lns3.scpt |
set optionsSection to optionsSection & LF2 & Indent1 & "-h, --help" & linefeed & Indent2 & "Print this help and exit." | lns3.scpt |
if booleanOptions does not contain "v" then | lns3.scpt |
set defaultOptions to defaultOptions & "v" | lns3.scpt |
set optionsSection to optionsSection & LF2 & Indent1 & "-v, --version" & linefeed & Indent2 & "Print version number and exit." | lns3.scpt |
set optionsSynopsis to "" | lns3.scpt |
if defaultOptions is not "" then set optionsSynopsis to optionsSynopsis & " [-" & defaultOptions & "]" | lns3.scpt |
if booleanOptions is not "" then set optionsSynopsis to optionsSynopsis & " [-" & booleanOptions & "]" | lns3.scpt |
if otherOptions is not "" then set optionsSynopsis to optionsSynopsis & otherOptions | lns3.scpt |
return {optionsSynopsis, optionsSection} | lns3.scpt |
end _formatOptions | lns3.scpt |
to _formatArguments(argumentDefinitions, vtStyle) | lns3.scpt |
if argumentDefinitions is {} then return {"", ""} | lns3.scpt |
set argumentsSynopsis to "" | lns3.scpt |
set argumentsSection to vtStyle's b & "ARGUMENTS" & vtStyle's n | lns3.scpt |
repeat with argumentRef in argumentDefinitions | lns3.scpt |
set argumentDefinition to (argumentRef as record) & {valueType:text, isList:false, defaultValue:NoValue, valuePlaceholder:"", valueDescription:""} | lns3.scpt |
set valueType to _asValueType(argumentDefinition's valueType) | lns3.scpt |
set isList to argumentDefinition's isList as boolean | lns3.scpt |
set valuePlaceholder to argumentDefinition's valuePlaceholder as text | lns3.scpt |
set valueDescription to _normalizeTrailingSpace(argumentDefinition's valueDescription as text) | lns3.scpt |
_support's throwInvalidParameterType(argumentDefinitions, "arguments", record, "list of ‘command line argument definition’ records") | lns3.scpt |
if valuePlaceholder is "" then set valuePlaceholder to _defaultValuePlaceholder(argumentDefinition) | lns3.scpt |
if isList then set valuePlaceholder to valuePlaceholder & " ..." | lns3.scpt |
set argumentsSection to argumentsSection & LF2 & Indent1 & valuePlaceholder | lns3.scpt |
if argumentDefinition's defaultValue is not NoValue then set valueDescription to valueDescription & _formatDefaultValue(argumentDefinition) | lns3.scpt |
if isList then set valueDescription to valueDescription & "This argument can appear multiple times. " | lns3.scpt |
if valueDescription is not "" then set argumentsSection to argumentsSection & linefeed & Indent2 & valueDescription | lns3.scpt |
if argumentDefinition's defaultValue is not NoValue then set valuePlaceholder to "[" & valuePlaceholder & "]" | lns3.scpt |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.