text
stringlengths 0
15.7k
| source
stringlengths 6
112
|
---|---|
set textRef's contents to "“" & (textRef's contents) & "”"
|
lns3.scpt
|
if enumList's length > 1 then set enumList's item -1 to "or " & enumList's item -1
|
lns3.scpt
|
return "This must be: " & _joinText(enumList, ", ") & ". "
|
lns3.scpt
|
end _formatEnumeration
|
lns3.scpt
|
to _buildOptionsTable(optionDefinitions)
|
lns3.scpt
|
set foundNames to {}
|
lns3.scpt
|
set optionDefinitionsByName to current application's NSMutableDictionary's dictionary()
|
lns3.scpt
|
repeat with optionRef in optionDefinitions
|
lns3.scpt
|
set optionDefinition to (optionRef's contents as record) & {shortName:"", longName:"", propertyName:"", valueType:text, isList:false, defaultValue:false} -- this defaultValue is only used by boolean opts
|
lns3.scpt
|
set propertyName to optionDefinition's propertyName as text
|
lns3.scpt
|
if optionDefinition's propertyName is "" then error number -1700
|
lns3.scpt
|
error "Invalid option definition (property name must be non-empty text)." number -1703 from optionRef
|
lns3.scpt
|
set nameCount to length of foundNames
|
lns3.scpt
|
repeat with aRef in {{optionDefinition's shortName, "-"}, {optionDefinition's longName, "--"}}
|
lns3.scpt
|
set {theName, namePrefix} to aRef
|
lns3.scpt
|
set theName to theName as text
|
lns3.scpt
|
error "Invalid option definition (short/long name must be text)." number -1700 from optionRef
|
lns3.scpt
|
if theName is not "" then
|
lns3.scpt
|
if {theName} is in foundNames then error "Invalid option definition (found duplicate short/long name)." number -1703 from optionRef
|
lns3.scpt
|
set end of foundNames to theName
|
lns3.scpt
|
if namePrefix's length = 1 then -- validate short name
|
lns3.scpt
|
if (theName's length ≠ 1 or theName is not in "abcdefghijklmnopqrstuvwxyz") then error "Invalid option definition (short name must be a single A-Z or a-z character)." number -1703 from optionRef
|
lns3.scpt
|
else -- validate long name
|
lns3.scpt
|
if theName's length = 0 or theName's character 1 is not in "abcdefghijklmnopqrstuvwxyz" or theName ends with "-" then error "Invalid option definition (long name must start with A-Z or a-z character)." number -1703 from optionRef
|
lns3.scpt
|
repeat with charRef in theName
|
lns3.scpt
|
if charRef's contents is not in "abcdefghijklmnopqrstuvwxyz1234567890-" then error "Invalid option definition (long name can only contain A-Z, a-z, 0-9 or hyphen characters)." number -1703 from optionRef
|
lns3.scpt
|
(optionDefinitionsByName's setObject:optionDefinition forKey:(namePrefix & theName))
|
lns3.scpt
|
if length of foundNames = nameCount then error "Invalid option definition (record must contain a non-empty ‘shortName’ and/or ‘longName’ property)." number -1703 from optionRef
|
lns3.scpt
|
return optionDefinitionsByName
|
lns3.scpt
|
end _buildOptionsTable
|
lns3.scpt
|
to _parseOptions(rawArguments, optionDefinitions, hasDefaultOptions)
|
lns3.scpt
|
set optionDefinitionsByName to _buildOptionsTable(optionDefinitions)
|
lns3.scpt
|
set asocParametersDict to current application's NSMutableDictionary's dictionary()
|
lns3.scpt
|
set AppleScript's text item delimiters to "=" -- note: a long option can use a space or '=' to separate its name and value
|
lns3.scpt
|
repeat until rawArguments is {}
|
lns3.scpt
|
set theArg to first item of rawArguments
|
lns3.scpt
|
if theArg starts with "--" then -- found "--[NAME[=VALUE]]" (NAME is a long option name)
|
lns3.scpt
|
if theArg is "--" then -- double-hypens terminates the option list, so anything left in rawArguments is positional arguments
|
lns3.scpt
|
set rawArguments to rest of rawArguments
|
lns3.scpt
|
set optionName to first text item of theArg -- get "--NAME"
|
lns3.scpt
|
if (count theArg each text item) > 1 then -- put "VALUE" back on stack for later
|
lns3.scpt
|
set first item of rawArguments to text (text item 2) thru -1 of theArg
|
lns3.scpt
|
else -- remove the option name from stack
|
lns3.scpt
|
else if theArg starts with "-" then -- found "-N[N…][VALUE]" (N is a single-character short option name)
|
lns3.scpt
|
if theArg is "-" or theArg's character 2 is in "01234567890" then exit repeat -- it's a lone hyphen or a negative number (i.e. not an option), so treat it and rest of rawArguments as positional arguments
|
lns3.scpt
|
set optionName to text 1 thru 2 of theArg -- get "-N"
|
lns3.scpt
|
if theArg's length > 2 then -- put "[-N…]VALUE" back on stack for later
|
lns3.scpt
|
set first item of rawArguments to text 3 thru -1 of theArg
|
lns3.scpt
|
set optionDefinition to optionDefinitionsByName's objectForKey:optionName
|
lns3.scpt
|
if optionDefinition is not missing value and (optionDefinition as any)'s valueType is boolean then
|
lns3.scpt
|
set first item of rawArguments to "-" & first item of rawArguments
|
lns3.scpt
|
else --remove the option name from stack
|
lns3.scpt
|
else -- not an option name, so anything left in rawArguments is positional arguments
|
lns3.scpt
|
if optionDefinition is missing value then -- check for default options (help/version), else raise error
|
lns3.scpt
|
if hasDefaultOptions then
|
lns3.scpt
|
set {isHelp, isVersion} to {{optionName} is in {"-h", "--help"}, {optionName} is in {"-v", "--version"}}
|
lns3.scpt
|
if isHelp or isVersion then -- ignore everything else and return a minimal record containing only `help` and `version` properties, one or both of which are true, so must be dealt with accordingly by `run` handler (i.e. format+log help text and return and/or return version number)
|
lns3.scpt
|
asocParametersDict's removeAllObjects()
|
lns3.scpt
|
asocParametersDict's setValue:isHelp forKey:"help"
|
lns3.scpt
|
asocParametersDict's setValue:isVersion forKey:"version"
|
lns3.scpt
|
set rawArguments to {}
|
lns3.scpt
|
error "Unknown option: " & optionName number _ArgvUserError
|
lns3.scpt
|
set optionDefinition to optionDefinition as any
|
lns3.scpt
|
set propertyName to optionDefinition's propertyName
|
lns3.scpt
|
if optionDefinition's valueType is boolean then
|
lns3.scpt
|
set theValue to not optionDefinition's defaultValue
|
lns3.scpt
|
error "Bad defaultValue for boolean option: " & optionName number -1700 from (a reference to defaultValue of optionDefinition)
|
lns3.scpt
|
if rawArguments is {} then error "Missing value for option: " & optionName number _ArgvUserError
|
lns3.scpt
|
set theValue to _unpackValue(first item of rawArguments, optionDefinition)
|
lns3.scpt
|
if optionDefinition's isList then -- option can appear multiple times, so collect in a list
|
lns3.scpt
|
set theList to asocParametersDict's objectForKey:propertyName
|
lns3.scpt
|
if theList is missing value then
|
lns3.scpt
|
set theValue to current application's NSMutableArray's arrayWithObject:theValue
|
lns3.scpt
|
theList's addObject:theValue
|
lns3.scpt
|
set theValue to theList
|
lns3.scpt
|
else if (asocParametersDict's objectForKey:propertyName) is not missing value then
|
lns3.scpt
|
error "Duplicate option: " & optionName number _ArgvUserError
|
lns3.scpt
|
asocParametersDict's setObject:theValue forKey:propertyName
|
lns3.scpt
|
return {asocParametersDict, rawArguments}
|
lns3.scpt
|
end _parseOptions
|
lns3.scpt
|
to _addDefaultOptions(asocParametersDict, optionDefinitions)
|
lns3.scpt
|
repeat with recRef in optionDefinitions
|
lns3.scpt
|
set rec to (recRef as record) & {propertyName:"", longName:"", defaultValue:NoValue}
|
lns3.scpt
|
set propertyName to rec's propertyName
|
lns3.scpt
|
if propertyName is "" then set propertyName to rec's longName
|
lns3.scpt
|
if (asocParametersDict's objectForKey:propertyName) is missing value then
|
lns3.scpt
|
set defaultValue to rec's defaultValue
|
lns3.scpt
|
if defaultValue is NoValue then -- record doesn't have a defaultValue property
|
lns3.scpt
|
if rec's valueType is boolean then
|
lns3.scpt
|
set defaultValue to false
|
lns3.scpt
|
set optionName to "--" & rec's longName
|
lns3.scpt
|
if optionName is "--" then set optionName to "-" & rec's shortName
|
lns3.scpt
|
error "Missing required option: " & optionName number _ArgvUserError
|
lns3.scpt
|
if defaultValue is missing value then set defaultValue to current application's NSNull's |null|()
|
lns3.scpt
|
(asocParametersDict's setObject:defaultValue forKey:propertyName)
|
lns3.scpt
|
repeat with propertyNameRef in {"help", "version"}
|
lns3.scpt
|
if (asocParametersDict's objectForKey:(propertyNameRef's contents)) is missing value then (asocParametersDict's setObject:false forKey:(propertyNameRef's contents))
|
lns3.scpt
|
end _addDefaultOptions
|
lns3.scpt
|
to _ordinalNumber(n)
|
lns3.scpt
|
if {n mod 10} is in {1, 2, 3} and {n mod 100} is not in {11, 12, 13} then
|
lns3.scpt
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.