function usage(settings) {
	// Allow this function to be used as the callback of an option.
	let args = []
	if (arguments.length === 3) {
		args = arguments[1]
		settings = arguments[2]
	}

	// Normalize the settings in case this function was called by the application.
	requireValid(args, settings)
	settings = _normalize(settings)

	let wrapWidth = settings.usage?.wrapWidth ?? process.stdout.columns ?? _defaultWrapWidth

	// Write the usage header.
	if (settings.usage?.header) {
		// Wrap the text on word boundaries.
		let header = settings.usage.header
		if (settings.usage.wrap) {
			header = header.match(/^\s*/)
				+ wordWrap(header, {
					indent: '',
					width: wrapWidth - 1
				})
				+ header.match(/\s*$/)
		}

		process.stdout.write(header)
	}

	// Write the usage specification.
	let program = settings.usage?.program ?? path.basename(process.argv[1]).split('.', 1)[0]
	process.stdout.write('Usage: ' + program)
	if (settings.usage?.spec)
		process.stdout.write(' ' + settings.usage.spec)
	process.stdout.write('\n')

	// Write the options.
	if (settings.options && settings.options.length > 0) {
		process.stdout.write('Options:\n')

		// Build the specification and description of each option.
		let options = settings.options
			.map(function(option) {
				let spec = []
					.concat(
						option.short
							.slice(0, settings.usage?.first ? 1 : Number.MAX_SAFE_INTEGER)
							.map(function(x) { return '-' + x }),
						option.long
							.slice(0, settings.usage?.first ? 1 : Number.MAX_SAFE_INTEGER)
							.map(function(x) { return '--' + x }))
					.join(' ')

				if (option.argument)
					spec += (option.long.length > 0 ? '=' : ' ')
						+ (option.optional ? '[' : '<')
						+ option.argument
						+ (option.optional ? ']' : '>')

				return {
					spec: spec,
					description: option.description
				}
			})

		// Calculate the maximum length of the options.
		let specLength = Math.max(...options.map(function(x) { return x.spec.length }))
		let descriptionLength = Math.max(...options.map(function(x) { return x.description.length }))

		// Write the options.
		for (let option of options) {
			// Wrap the description on word boundaries.
			let description = option.description
			if (settings.usage?.wrap && specLength + descriptionLength + 3 > wrapWidth) {
				description = ' '.repeat(specLength + 3) + description
				description = wordWrap(description, {
					indent: ' '.repeat(specLength + 3),
					width: wrapWidth - 1
				})
				description = description.substring((specLength + 3) * 2)
			}

			process.stdout.write('  ' + option.spec.padEnd(specLength) + ' ' + description + '\n')
		}
	}

	// Write the usage footer.
	if (settings.usage?.footer) {
		// Wrap the text on word boundaries.
		let footer = settings.usage.footer
		if (settings.usage?.wrap) {
			footer = footer.match(/^\s*/)
				+ wordWrap(footer, {
					indent: '',
					width: wrapWidth - 1
				})
				+ footer.match(/\s*$/)
		}

		process.stdout.write(footer)
	}

	process.exit()
}
/**
 * Displays the usage information and exits.
 * @param {getopt~Settings} [settings] The configuration of the parser.
 */
Displays the usage information and exits.
@param {getopt~Settings} [settings] The configuration of the parser.
Displays the usage information and exits . @param { getopt~Settings } [ settings ] The configuration of the parser .
function usage(settings) {
	let args = []
	if (arguments.length === 3) {
		args = arguments[1]
		settings = arguments[2]
	}
	requireValid(args, settings)
	settings = _normalize(settings)
	let wrapWidth = settings.usage?.wrapWidth ?? process.stdout.columns ?? _defaultWrapWidth
	if (settings.usage?.header) {
		let header = settings.usage.header
		if (settings.usage.wrap) {
			header = header.match(/^\s*/)
				+ wordWrap(header, {
					indent: '',
					width: wrapWidth - 1
				})
				+ header.match(/\s*$/)
		}
		process.stdout.write(header)
	}
	let program = settings.usage?.program ?? path.basename(process.argv[1]).split('.', 1)[0]
	process.stdout.write('Usage: ' + program)
	if (settings.usage?.spec)
		process.stdout.write(' ' + settings.usage.spec)
	process.stdout.write('\n')
	if (settings.options && settings.options.length > 0) {
		process.stdout.write('Options:\n')
		let options = settings.options
			.map(function(option) {
				let spec = []
					.concat(
						option.short
							.slice(0, settings.usage?.first ? 1 : Number.MAX_SAFE_INTEGER)
							.map(function(x) { return '-' + x }),
						option.long
							.slice(0, settings.usage?.first ? 1 : Number.MAX_SAFE_INTEGER)
							.map(function(x) { return '--' + x }))
					.join(' ')
				if (option.argument)
					spec += (option.long.length > 0 ? '=' : ' ')
						+ (option.optional ? '[' : '<')
						+ option.argument
						+ (option.optional ? ']' : '>')
				return {
					spec: spec,
					description: option.description
				}
			})
		let specLength = Math.max(...options.map(function(x) { return x.spec.length }))
		let descriptionLength = Math.max(...options.map(function(x) { return x.description.length }))
		for (let option of options) {
			let description = option.description
			if (settings.usage?.wrap && specLength + descriptionLength + 3 > wrapWidth) {
				description = ' '.repeat(specLength + 3) + description
				description = wordWrap(description, {
					indent: ' '.repeat(specLength + 3),
					width: wrapWidth - 1
				})
				description = description.substring((specLength + 3) * 2)
			}
			process.stdout.write('  ' + option.spec.padEnd(specLength) + ' ' + description + '\n')
		}
	}
	if (settings.usage?.footer) {
		let footer = settings.usage.footer
		if (settings.usage?.wrap) {
			footer = footer.match(/^\s*/)
				+ wordWrap(footer, {
					indent: '',
					width: wrapWidth - 1
				})
				+ footer.match(/\s*$/)
		}
		process.stdout.write(footer)
	}
	process.exit()
}
function usage ( settings ) { let args = [ ] if ( arguments . length === 3 ) { args = arguments [ 1 ] settings = arguments [ 2 ] } requireValid ( args , settings ) settings = _normalize ( settings ) let wrapWidth = settings . usage ?. wrapWidth ?? process . stdout . columns ?? _defaultWrapWidth if ( settings . usage ?. header ) { let header = settings . usage . header if ( settings . usage . wrap ) { header = header . match ( / ^\s* / ) + wordWrap ( header , { indent : '' , width : wrapWidth - 1 } ) + header . match ( / \s*$ / ) } process . stdout . write ( header ) } let program = settings . usage ?. program ?? path . basename ( process . argv [ 1 ] ) . split ( '.' , 1 ) [ 0 ] process . stdout . write ( 'Usage: ' + program ) if ( settings . usage ?. spec ) process . stdout . write ( ' ' + settings . usage . spec ) process . stdout . write ( '\n' ) if ( settings . options && settings . options . length > 0 ) { process . stdout . write ( 'Options:\n' ) let options = settings . options . map ( function ( option ) { let spec = [ ] . concat ( option . short . slice ( 0 , settings . usage ?. first ? 1 : Number . MAX_SAFE_INTEGER ) . map ( function ( x ) { return '-' + x } ) , option . long . slice ( 0 , settings . usage ?. first ? 1 : Number . MAX_SAFE_INTEGER ) . map ( function ( x ) { return '--' + x } ) ) . join ( ' ' ) if ( option . argument ) spec += ( option . long . length > 0 ? '=' : ' ' ) + ( option . optional ? '[' : '<' ) + option . argument + ( option . optional ? ']' : '>' ) return { spec : spec , description : option . description } } ) let specLength = Math . max ( ... options . map ( function ( x ) { return x . spec . length } ) ) let descriptionLength = Math . max ( ... options . map ( function ( x ) { return x . description . length } ) ) for ( let option of options ) { let description = option . description if ( settings . usage ?. wrap && specLength + descriptionLength + 3 > wrapWidth ) { description = ' ' . repeat ( specLength + 3 ) + description description = wordWrap ( description , { indent : ' ' . repeat ( specLength + 3 ) , width : wrapWidth - 1 } ) description = description . substring ( ( specLength + 3 ) * 2 ) } process . stdout . write ( '  ' + option . spec . padEnd ( specLength ) + ' ' + description + '\n' ) } } if ( settings . usage ?. footer ) { let footer = settings . usage . footer if ( settings . usage ?. wrap ) { footer = footer . match ( / ^\s* / ) + wordWrap ( footer , { indent : '' , width : wrapWidth - 1 } ) + footer . match ( / \s*$ / ) } process . stdout . write ( footer ) } process . exit ( ) }
Displays the usage information and exits.
Displays the usage information and exits .
// Allow this function to be used as the callback of an option. // Normalize the settings in case this function was called by the application. // Write the usage header. // Wrap the text on word boundaries. // Write the usage specification. // Write the options. // Build the specification and description of each option. // Calculate the maximum length of the options. // Write the options. // Wrap the description on word boundaries. // Write the usage footer. // Wrap the text on word boundaries.