From 57e41e0150cc379283e0763e46eda98427ce3a64 Mon Sep 17 00:00:00 2001 From: AndreasMaros Date: Thu, 3 Mar 2022 20:27:37 +0100 Subject: [PATCH] [refactor] Normalized OptionParameters object; rename command to "mode" --- src/argument-parser.dart | 36 +++---- src/configuration/config.dart | 186 +++++++++++++++------------------- src/configuration/wizard.dart | 2 +- 3 files changed, 102 insertions(+), 122 deletions(-) diff --git a/src/argument-parser.dart b/src/argument-parser.dart index 8eaada4..6eb4a2c 100644 --- a/src/argument-parser.dart +++ b/src/argument-parser.dart @@ -13,29 +13,29 @@ class ArgumentParser { ArgResults parseOptions(Iterable args) { var params = Map.from(OptionParameters); - params.forEach((option, config) { - if (config['type'] == 'separator') { - parser.addSeparator(config['content']); - return; + OptionParameters.forEach((ConfigurationOption option, OptionParameter config) { + if (config.separatorBefore is String) { + parser.addSeparator(config.separatorBefore!); } - var type = config['default'].runtimeType; + bool isBoolean = config.defaultValue is bool; - if (type == bool) { + if (isBoolean) { parser.addFlag( - config['long'], - abbr: config['short'], - defaultsTo: config['default'], - negatable: false, - help: config['help']); - } else if (type == String || config['default'] == null) { + config.long, + abbr: config.short, + defaultsTo: config.defaultValue, + negatable: false, + help: config.help + ); + } else { parser.addOption( - config['long'], - abbr: config['short'], - defaultsTo: config['default'], - help: config['help'], - valueHelp: config['valueHelp'], - ); + config.long, + abbr: config.short, + defaultsTo: config.defaultValue, + help: config.help, + valueHelp: config.valueHelp, + ); } }); diff --git a/src/configuration/config.dart b/src/configuration/config.dart index dd711e0..2bad240 100644 --- a/src/configuration/config.dart +++ b/src/configuration/config.dart @@ -28,117 +28,97 @@ enum ConfigurationOption { noInteraction, } -final OptionParameters = const { - 'sep-general': { - 'type': 'separator', - 'content': '--- General help & information --------' - }, - ConfigurationOption.help: { - 'long': 'help', - 'short': 'h', - 'default': false, - 'help': 'Show this help and exit.' - }, - ConfigurationOption.version: { - 'long': 'version', - 'short': 'v', - 'default': false, - 'help': 'Display version and exit.' - }, - 'sep-global': { - 'type': 'separator', - 'content': '--- Global settings -------------------' - }, - ConfigurationOption.projectDirectory: { - 'long': 'directory', - 'short': 'd', - // 'default': '\$HOME', - 'valueHelp': 'path-to-project', - 'help': ''' +class OptionParameter { + final String? separatorBefore; + final String long; + final String short; + final defaultValue; + final String? help; + final String? valueHelp; + + const OptionParameter( + this.long, + this.short, + { + this.defaultValue = null, + this.help = null, + this.valueHelp = null, + this.separatorBefore = null, + } + ); +} + +const Map OptionParameters = const { + ConfigurationOption.help: OptionParameter('help', 'h', + separatorBefore: '--- General help & information --------', + defaultValue: false, + help: 'Show this help and exit.' + ), + ConfigurationOption.version: OptionParameter('version', 'v', + defaultValue: false, + help: 'Display version and exit', + ), + ConfigurationOption.projectDirectory: OptionParameter('directory', 'd', + valueHelp: '', + help: ''' The directory to run in. This is the directory that contains backup/, userdata/ -and public/ directoris. Defaults to the current working directory in +and public/ directories. Defaults to the current working directory in no-interaction mode. ''', - }, - ConfigurationOption.verbose: { - 'long': 'verbose', - 'short': 'V', - 'default': false, - 'help': 'More output.' - }, - ConfigurationOption.backupUser: { - 'long': 'user', - 'short': 'u', - 'default': null, - 'help': 'The user under which to operate.', - 'valueHelp': 'blargo' - }, - ConfigurationOption.noInteraction: { - 'long': 'no-interaction', - 'short': 'n', - 'default': false, - 'help': - 'Skip the wizard, do not ask questions. For scripts and CI applications.' - }, - 'sep-operation': { - 'type': 'separator', - 'content': '--- Operation control -----------------' - }, - ConfigurationOption.operation: { - 'long': 'operation', - 'short': 'o', - 'help': 'Which backups to perform', - 'valueHelp': 'database|userdata|all' - }, - ConfigurationOption.operationAll: { - 'long': 'all', - 'short': 'a', - 'default': false, - }, - ConfigurationOption.operationDb: { - 'long': 'db', - 'short': 'D', - 'default': false, - }, - ConfigurationOption.operationUserdata: { - 'long': 'userdata', - 'short': 'U', - 'default': false, - }, - 'sep-database': { - 'type': 'separator', - 'content': '--- Database backup settings ----------', - }, - ConfigurationOption.usePhpWrapper: { - 'long': 'php-wrapper', - 'short': 'P', - 'default': false, - 'help': ''' + ), + ConfigurationOption.verbose: OptionParameter('verbose', 'V', + defaultValue: false, + help: 'More output', + ), + ConfigurationOption.backupUser: OptionParameter('user', 'u', + defaultValue: null, + help: 'The user under which to operate.', + valueHelp: '', + ), + ConfigurationOption.noInteraction: OptionParameter('no-interaction', 'n', + defaultValue: false, + help: 'Skip the wizard, do not ask questions. For scripts and CI applications.', + ), + ConfigurationOption.operation: OptionParameter('operation', 'o', + help: 'Which backups to perform', + valueHelp: 'database|userdata|all', + separatorBefore: '--- Operation control -----------------', + ), + ConfigurationOption.operationAll: OptionParameter('all', 'a', + defaultValue: false, + ), + ConfigurationOption.operationDb: OptionParameter('db', 'D', defaultValue: false), + ConfigurationOption.operationUserdata: OptionParameter('userdata', 'U', defaultValue: false), + ConfigurationOption.usePhpWrapper: OptionParameter('php-wrapper', 'P', + separatorBefore: '--- Database backup settings ----------', + defaultValue: false, + help: ''' Instead of using a (custom) PHP binary, use php-wrapper instead (must be on the \$PATH). ''', - }, - ConfigurationOption.phpBinary: {'long': 'php', 'short': 'p', 'default': null}, - ConfigurationOption.wpBinaryType: { - 'long': 'wp-cli-type', - 'short': 'w', - 'help': ''' + ), + ConfigurationOption.phpBinary: OptionParameter('php', 'p', defaultValue: null), + ConfigurationOption.wpBinaryType: OptionParameter('wp-cli-type', 'w', + valueHelp: 'bundled|path|directory', + help: ''' Whether to use a wp-cli binary on the \$PATH, in the script directory or the bundled version (depending on version). For a custom path, use --wp--cli-path. - ''', - 'valueHelp': 'bundled|path|directory', - }, - ConfigurationOption.wpBinary: { - 'long': 'wp-cli-path', - 'short': 'W', - 'default': null, - 'help': 'A custom wp-cli executable PHAR archive to use for DB exporting.', - 'valueHelp': 'path-to-wp-cli.phar', - }, + ''', + ), + ConfigurationOption.wpBinary: OptionParameter('wp-cli-path', 'W', + defaultValue: null, + help: 'A custom wp-cli executable PHAR archive to use for DB exporting.', + valueHelp: 'path-to-wp-cli.phar', + ), + ConfigurationOption.comment: OptionParameter('comment', 'c', + defaultValue: null, + help: 'A comment or tag to append to the backup file (not available in restore mode).', + valueHelp: '', + ), }; class Config { - String? command; + String? mode; OperationType? operation; @@ -162,7 +142,7 @@ class Config { parseOperation(); if (options.command?.name != null) { - command = options.command!.name; + mode = options.command!.name; } if (getParameterValue(ConfigurationOption.projectDirectory) != null) { @@ -221,8 +201,8 @@ class Config { } } - static getParameter(ConfigurationOption option) { - return OptionParameters[option]!['long']; + static String getParameter(ConfigurationOption option) { + return OptionParameters[option]!.long; } getParameterValue(ConfigurationOption option) { diff --git a/src/configuration/wizard.dart b/src/configuration/wizard.dart index 0beb4ce..4944401 100644 --- a/src/configuration/wizard.dart +++ b/src/configuration/wizard.dart @@ -25,7 +25,7 @@ final Map ProjectPathPresetOptions = const { ProjectPathPreset.custom: 'A custom path', }; -final Map WpCliPresetOptions = const { +final Map WpCliPresetOptions = const { WpCliType.bundled: 'Use the bundled PHAR archive of WP-CLI (recommended)', WpCliType.path: 'Use the executable PHAR named "wp" on the \$PATH', WpCliType.directory: