MCPcopy Create free account
hub / github.com/SmartThingsCommunity/smartthings-cli / internalIntegerInput

Function internalIntegerInput

src/lib/user-query.ts:86–127  ·  view source on GitHub ↗
(
		message: string,
		options: IntegerInputOptions<number | undefined> & { required: boolean },
)

Source from the content-addressed store, hash-verified

84}
85
86const internalIntegerInput = async (
87 message: string,
88 options: IntegerInputOptions<number | undefined> & { required: boolean },
89): Promise<number | undefined> => {
90 const validate = (input: string): true | string => {
91 if (options?.helpText && input === '?') {
92 return true
93 }
94
95 if (!options.required && input === '') {
96 return true
97 }
98
99 if (!input.match(/^-?\d+$/)) {
100 return `"${input}" is not a valid integer`
101 }
102
103 if (!options.validate) {
104 return true
105 }
106
107 return options.validate(Number(input))
108 }
109
110 // Using `inquirer`'s `number` function instead of `input` would be nice but it doesn't allow
111 // us to accept `?` for help text.
112 const prompt = async (): Promise<string | undefined> => await input({
113 message: options.helpText ? `${message} (? for help)` : message,
114 transformer: displayNoneForEmpty,
115 validate,
116 default: (typeof options.default === 'function' ? options.default() : options.default)?.toString(),
117 required: options.required,
118 })
119
120 let entered = await prompt()
121 while (options.helpText && entered === '?') {
122 console.log(options.helpText)
123 entered = await prompt()
124 }
125
126 return entered ? Number(entered) : undefined
127}
128
129/**
130 * Prompt the user for an optional integer. The return value will always be a number that is an

Callers 2

optionalIntegerInputFunction · 0.85
integerInputFunction · 0.85

Calls 1

promptFunction · 0.85

Tested by

no test coverage detected