htpdate/argv.coffee

127 lines
3.4 KiB
CoffeeScript
Raw Normal View History

2021-01-25 05:20:52 +01:00
path = require 'path'
util = require 'util'
minimist = require 'minimist'
minimist_opt = require 'minimist-options'
{ orderBy } = require 'natural-orderby'
info = require './package.json'
platform = require('os').platform()
DEFAULT_OPTIONS = {
help: {
describe: 'This help text'
type: 'boolean'
alias: 'h'
default: false
}
version: {
describe: "display the version of #{info.name} and exit"
type: 'boolean'
alias: 'v'
default: false
}
}
print_version = ->
console.log """
#{info.name} #{info.version}, #{info.description}
License: AGPL-3.0
Copyright (C) 2021 Bob Wen. All rights reserved.
Homepage: #{info.homepage}
2021-01-25 05:20:52 +01:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation; either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
Libraries:
"""
for lib_name, version of info.dependencies
lib_info = require "./node_modules/#{lib_name}/package.json"
console.log """
#{lib_info.name} #{lib_info.version}
License: #{lib_info.license}
Homepage: #{lib_info.homepage.split('#')[0]}
"""
print_usage = (options) ->
items = []
for k, v of options
items.push {
key: k
v...
}
items = orderBy items, (v) -> v.alias ? v.key
console.log """
#{info.name} #{info.version}, #{info.description}
2021-01-25 09:25:06 +01:00
Usage: #{info.name} [options...] URLs...
2021-01-25 05:20:52 +01:00
Options
"""
for o in items
if o.alias?
line = " -#{o.alias}, --#{o.key}".padEnd 24
else
line = " --#{o.key}".padEnd 24
line += "#{o.describe or ''}\n"
if o.default?
line += "".padStart(24) + "Default: #{util.inspect o.default}"
else if o.type?
line += "".padStart(24) + "Type: #{o.type}"
console.log line + '\n'
print_examples = (exam = []) ->
console.log """
Examples
2021-01-25 07:48:42 +01:00
Synchronize time from multiple URLs
#{info.name} -s www.pool.ntp.org www.openssl.org nodejs.org
Query time from multiple URLs
2021-01-25 05:20:52 +01:00
#{info.name} www.pool.ntp.org www.openssl.org nodejs.org
Change default protocol to 'http'
2021-01-25 07:48:42 +01:00
#{info.name} -s -p http www.pool.ntp.org
2021-01-25 05:20:52 +01:00
2021-01-25 09:25:06 +01:00
Mix http and https URLs
2021-01-25 07:48:42 +01:00
#{info.name} -s http://www.pool.ntp.org https://www.openssl.org
2021-01-25 05:20:52 +01:00
Access through a http proxy
#{if platform is 'win32' then 'set' else 'export'} http_proxy=http://127.0.0.1:8118
2021-01-25 07:48:42 +01:00
#{info.name} -s www.pool.ntp.org
2021-01-25 05:20:52 +01:00
"""
module.exports = (opt, exam) ->
cli_options = {
DEFAULT_OPTIONS...
opt...
}
argv = minimist process.argv.slice(2), minimist_opt cli_options
if argv.version
print_version()
process.exit 0
if argv.help or argv._.length is 0
print_usage cli_options
print_examples exam
console.log()
if argv.help
process.exit 0
2021-01-25 09:25:06 +01:00
console.error "\nError: Missing server URL, at least one URL should be specified"
2021-01-25 05:20:52 +01:00
process.exit 0
if argv.count < 1
argv.count = 1
2021-01-29 04:35:57 +01:00
if argv.retry < 0
argv.retry = 0
2021-01-25 05:20:52 +01:00
argv