Permalink
Browse files

make parse method static and add options_to_parser method to reuse op…

…tions easier
  • Loading branch information...
1 parent 275c47a commit 1b429a383de8957e119e36727dadd5ed6d34a3db @wandenberg wandenberg committed Mar 19, 2012
Showing with 124 additions and 126 deletions.
  1. +1 −2 simple_db_migrate/__init__.py
  2. +60 −61 simple_db_migrate/cli.py
  3. +59 −59 tests/cli_test.py
  4. +4 −4 tests/run_test.py
@@ -13,9 +13,8 @@
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
def run(args=None):
- cli = CLI()
try:
- (options, args) = cli.parse(args)
+ (options, args) = CLI.parse(args)
if options.simple_db_migrate_version:
msg = 'simple-db-migrate v%s' % SIMPLE_DB_MIGRATE_VERSION
View
@@ -1,4 +1,4 @@
-from optparse import OptionParser
+from optparse import OptionParser, make_option
import sys
class CLI(object):
@@ -25,143 +25,142 @@ def show_colors():
"END": "\033[0m",
}
- def __init__(self):
- self.__config_parser()
-
- def __config_parser(self):
- self.__parser = OptionParser()
+ @staticmethod
+ def parse(args=None):
+ parser = OptionParser(option_list=CLI.options_to_parser())
+ parser.add_option("-v", "--version",
+ action="store_true",
+ dest="simple_db_migrate_version",
+ default=False,
+ help="Displays simple-db-migrate's version and exit.")
+ return parser.parse_args(args)
- self.__parser.add_option("-c", "--config",
+ @classmethod
+ def options_to_parser(cls):
+ return (
+ make_option("-c", "--config",
dest="config_file",
default=None,
- help="Use a specific config file. If not provided, will search for 'simple-db-migrate.conf' in the current directory.")
+ help="Use a specific config file. If not provided, will search for 'simple-db-migrate.conf' in the current directory."),
- self.__parser.add_option("-l", "--log-level",
+ make_option("-l", "--log-level",
dest="log_level",
default=1,
- help="Log level: 0-no log; 1-migrations log; 2-statement execution log (default: %default)")
+ help="Log level: 0-no log; 1-migrations log; 2-statement execution log (default: %default)"),
- self.__parser.add_option("--log-dir",
+ make_option("--log-dir",
dest="log_dir",
default=None,
- help="Directory to save the log files of execution")
+ help="Directory to save the log files of execution"),
- self.__parser.add_option("--force-old-migrations", "--force-execute-old-migrations-versions",
+ make_option("--force-old-migrations", "--force-execute-old-migrations-versions",
action="store_true",
dest="force_execute_old_migrations_versions",
default=False,
- help="Forces the use of the old migration files even if the destination version is the same as current destination ")
+ help="Forces the use of the old migration files even if the destination version is the same as current destination "),
- self.__parser.add_option("--force-files", "--force-use-files-on-down",
+ make_option("--force-files", "--force-use-files-on-down",
action="store_true",
dest="force_use_files_on_down",
default=False,
- help="Forces the use of the migration files instead of using the field sql_down stored on the version table in database downgrade operations ")
+ help="Forces the use of the migration files instead of using the field sql_down stored on the version table in database downgrade operations "),
- self.__parser.add_option("-m", "--migration",
+ make_option("-m", "--migration",
dest="schema_version",
default=None,
- help="Schema version to migrate to. If not provided will migrate to the last version available in the migrations directory.")
+ help="Schema version to migrate to. If not provided will migrate to the last version available in the migrations directory."),
- self.__parser.add_option("-n", "--create", "--new",
+ make_option("-n", "--create", "--new",
dest="new_migration",
default=None,
- help="Create migration file with the given nickname. The nickname should contain only lowercase characters and underscore '_'. Example: 'create_table_xyz'.")
+ help="Create migration file with the given nickname. The nickname should contain only lowercase characters and underscore '_'. Example: 'create_table_xyz'."),
- self.__parser.add_option("-p", "--paused-mode",
+ make_option("-p", "--paused-mode",
action="store_true",
dest="paused_mode",
default=False,
- help="Execute in 'paused' mode. In this mode you will need to press <enter> key in order to execute each SQL command, making it easier to see what is being executed and helping debug. When paused mode is enabled, log level is automatically set to [2].")
+ help="Execute in 'paused' mode. In this mode you will need to press <enter> key in order to execute each SQL command, making it easier to see what is being executed and helping debug. When paused mode is enabled, log level is automatically set to [2]."),
- self.__parser.add_option("-v", "--version",
- action="store_true",
- dest="simple_db_migrate_version",
- default=False,
- help="Displays simple-db-migrate's version and exit.")
-
- self.__parser.add_option("--color",
+ make_option("--color",
action="store_true",
dest="show_colors",
default=False,
- help="Output with beautiful colors.")
+ help="Output with beautiful colors."),
- self.__parser.add_option("--drop", "--drop-database-first",
+ make_option("--drop", "--drop-database-first",
action="store_true",
dest="drop_db_first",
default=False,
- help="Drop database before running migrations to create everything from scratch. Useful when the database schema is corrupted and the migration scripts are not working.")
+ help="Drop database before running migrations to create everything from scratch. Useful when the database schema is corrupted and the migration scripts are not working."),
- self.__parser.add_option("--show-sql",
+ make_option("--show-sql",
action="store_true",
dest="show_sql",
default=False,
- help="Show all SQL statements executed.")
+ help="Show all SQL statements executed."),
- self.__parser.add_option("--show-sql-only",
+ make_option("--show-sql-only",
action="store_true",
dest="show_sql_only",
default=False,
- help="Show all SQL statements that would be executed but DON'T execute them in the database.")
+ help="Show all SQL statements that would be executed but DON'T execute them in the database."),
- self.__parser.add_option("--label",
+ make_option("--label",
dest="label_version",
default=None,
- help="Give this label the migrations executed or execute a down to him.")
+ help="Give this label the migrations executed or execute a down to him."),
- self.__parser.add_option("--password",
+ make_option("--password",
dest="password",
default=None,
- help="Use this password to connect to database, to auto.")
+ help="Use this password to connect to database, to auto."),
- self.__parser.add_option("--env", "--environment",
+ make_option("--env", "--environment",
dest="environment",
default="",
- help="Use this environment to get specific configurations.")
+ help="Use this environment to get specific configurations."),
- self.__parser.add_option("--utc-timestamp",
+ make_option("--utc-timestamp",
action="store_true",
dest="utc_timestamp",
default=False,
- help="Use utc datetime value on the name of migration when creating one.")
+ help="Use utc datetime value on the name of migration when creating one."),
- self.__parser.add_option("--db-engine",
+ make_option("--db-engine",
dest="database_engine",
default=None,
- help="Set each engine to use as sgdb (mysql, oracle, mssql). (default: 'mysql')")
+ help="Set each engine to use as sgdb (mysql, oracle, mssql). (default: 'mysql')"),
- self.__parser.add_option("--db-version-table",
+ make_option("--db-version-table",
dest="database_version_table",
default=None,
- help="Set the name of the table used to save migrations history. (default: '__db_version__')")
+ help="Set the name of the table used to save migrations history. (default: '__db_version__')"),
- self.__parser.add_option("--db-user",
+ make_option("--db-user",
dest="database_user",
default=None,
- help="Set the username to connect to database.")
+ help="Set the username to connect to database."),
- self.__parser.add_option("--db-password",
+ make_option("--db-password",
dest="database_password",
default=None,
- help="Set the password to connect to database.")
+ help="Set the password to connect to database."),
- self.__parser.add_option("--db-host",
+ make_option("--db-host",
dest="database_host",
default=None,
- help="Set the host where the database is.")
+ help="Set the host where the database is."),
- self.__parser.add_option("--db-name",
+ make_option("--db-name",
dest="database_name",
default=None,
- help="Set the name of the database.")
+ help="Set the name of the database."),
- self.__parser.add_option("--db-migrations-dir",
+ make_option("--db-migrations-dir",
dest="database_migrations_dir",
default=None,
- help="List of directories where migrations are separated by a colon")
-
- def parse(self, args=None):
- return self.__parser.parse_args(args)
+ help="List of directories where migrations are separated by a colon"),
+ )
@classmethod
def error_and_exit(cls, msg):
Oops, something went wrong.

0 comments on commit 1b429a3

Please sign in to comment.