Obsolete "optparse" library replaced by "argparse"

This commit is contained in:
Vulcalien 2021-09-09 16:05:39 +02:00
parent 8f41308cb2
commit 4d00f83084
1 changed files with 37 additions and 50 deletions

View File

@ -15,11 +15,10 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Terminator.optionparse - Parse commandline options""" """Terminator.optionparse - Parse commandline options"""
import argparse
import sys import sys
import os import os
# FIXME optparse is deprecated - argparse be used instead
from optparse import OptionParser, SUPPRESS_HELP
from terminatorlib.terminator import Terminator from terminatorlib.terminator import Terminator
from .util import dbg, err from .util import dbg, err
from . import util from . import util
@ -29,97 +28,86 @@ from .translation import _
options = None options = None
def execute_cb(option, opt, value, lparser): class ExecuteCallback(argparse.Action):
def __call__(self, lparser, namespace, values, option_string=None):
"""Callback for use in parsing execute options""" """Callback for use in parsing execute options"""
assert value is None setattr(namespace, self.dest, values)
value = []
while lparser.rargs:
arg = lparser.rargs[0]
value.append(arg)
del(lparser.rargs[0])
setattr(lparser.values, option.dest, value)
def parse_options(): def parse_options():
"""Parse the command line options""" """Parse the command line options"""
usage = "usage: %prog [options]"
is_x_terminal_emulator = os.path.basename(sys.argv[0]) == 'x-terminal-emulator' is_x_terminal_emulator = os.path.basename(sys.argv[0]) == 'x-terminal-emulator'
parser = OptionParser(usage) parser = argparse.ArgumentParser()
parser.add_option('-v', '--version', action='store_true', dest='version', parser.add_argument('-v', '--version', action='store_true', dest='version',
help=_('Display program version')) help=_('Display program version'))
parser.add_option('-m', '--maximise', action='store_true', dest='maximise', parser.add_argument('-m', '--maximise', action='store_true', dest='maximise',
help=_('Maximize the window')) help=_('Maximize the window'))
parser.add_option('-M', '--maximize', action='store_true', dest='maximise', parser.add_argument('-M', '--maximize', action='store_true', dest='maximise',
help=_('Maximize the window')) help=_('Maximize the window'))
parser.add_option('-f', '--fullscreen', action='store_true', parser.add_argument('-f', '--fullscreen', action='store_true',
dest='fullscreen', help=_('Make the window fill the screen')) dest='fullscreen', help=_('Make the window fill the screen'))
parser.add_option('-b', '--borderless', action='store_true', parser.add_argument('-b', '--borderless', action='store_true',
dest='borderless', help=_('Disable window borders')) dest='borderless', help=_('Disable window borders'))
parser.add_option('-H', '--hidden', action='store_true', dest='hidden', parser.add_argument('-H', '--hidden', action='store_true', dest='hidden',
help=_('Hide the window at startup')) help=_('Hide the window at startup'))
parser.add_option('-T', '--title', dest='forcedtitle', parser.add_argument('-T', '--title', dest='forcedtitle',
help=_('Specify a title for the window')) help=_('Specify a title for the window'))
parser.add_option('--geometry', dest='geometry', type='string', parser.add_argument('--geometry', dest='geometry', type=str,
help=_('Set the preferred size and position of the window' help=_('Set the preferred size and position of the window'
'(see X man page)')) '(see X man page)'))
if not is_x_terminal_emulator: if not is_x_terminal_emulator:
parser.add_option('-e', '--command', dest='command', parser.add_argument('-e', '--command', dest='command',
help=_('Specify a command to execute inside the terminal')) help=_('Specify a command to execute inside the terminal'))
else: else:
parser.add_option('--command', dest='command', parser.add_argument('--command', dest='command',
help=_('Specify a command to execute inside the terminal')) help=_('Specify a command to execute inside the terminal'))
parser.add_option('-e', '--execute2', dest='execute', action='callback', parser.add_argument('-e', '--execute2', dest='execute', action=ExecuteCallback,
callback=execute_cb,
help=_('Use the rest of the command line as a command to ' help=_('Use the rest of the command line as a command to '
'execute inside the terminal, and its arguments')) 'execute inside the terminal, and its arguments'))
parser.add_option('-g', '--config', dest='config', parser.add_argument('-g', '--config', dest='config',
help=_('Specify a config file')) help=_('Specify a config file'))
parser.add_option('-j', '--config-json', dest='configjson', parser.add_argument('-j', '--config-json', dest='configjson',
help=_('Specify a partial config json file')) help=_('Specify a partial config json file'))
parser.add_option('-x', '--execute', dest='execute', action='callback', parser.add_argument('-x', '--execute', dest='execute', action=ExecuteCallback,
callback=execute_cb,
help=_('Use the rest of the command line as a command to execute ' help=_('Use the rest of the command line as a command to execute '
'inside the terminal, and its arguments')) 'inside the terminal, and its arguments'))
parser.add_option('--working-directory', metavar='DIR', parser.add_argument('--working-directory', metavar='DIR',
dest='working_directory', help=_('Set the working directory')) dest='working_directory', help=_('Set the working directory'))
parser.add_option('-i', '--icon', dest='forcedicon', help=_('Set a custom \ parser.add_argument('-i', '--icon', dest='forcedicon', help=_('Set a custom \
icon for the window (by file or name)')) icon for the window (by file or name)'))
parser.add_option('-r', '--role', dest='role', parser.add_argument('-r', '--role', dest='role',
help=_('Set a custom WM_WINDOW_ROLE property on the window')) help=_('Set a custom WM_WINDOW_ROLE property on the window'))
parser.add_option('-l', '--layout', dest='layout', parser.add_argument('-l', '--layout', dest='layout',
help=_('Launch with the given layout')) help=_('Launch with the given layout'))
parser.add_option('-s', '--select-layout', action='store_true', parser.add_argument('-s', '--select-layout', action='store_true',
dest='select', help=_('Select a layout from a list')) dest='select', help=_('Select a layout from a list'))
parser.add_option('-p', '--profile', dest='profile', parser.add_argument('-p', '--profile', dest='profile',
help=_('Use a different profile as the default')) help=_('Use a different profile as the default'))
parser.add_option('-u', '--no-dbus', action='store_true', dest='nodbus', parser.add_argument('-u', '--no-dbus', action='store_true', dest='nodbus',
help=_('Disable DBus')) help=_('Disable DBus'))
parser.add_option('-d', '--debug', action='count', dest='debug', parser.add_argument('-d', '--debug', action='count', dest='debug',
help=_('Enable debugging information (twice for debug server)')) help=_('Enable debugging information (twice for debug server)'))
parser.add_option('--debug-classes', action='store', dest='debug_classes', parser.add_argument('--debug-classes', action='store', dest='debug_classes',
help=_('Comma separated list of classes to limit debugging to')) help=_('Comma separated list of classes to limit debugging to'))
parser.add_option('--debug-methods', action='store', dest='debug_methods', parser.add_argument('--debug-methods', action='store', dest='debug_methods',
help=_('Comma separated list of methods to limit debugging to')) help=_('Comma separated list of methods to limit debugging to'))
parser.add_option('--new-tab', action='store_true', dest='new_tab', parser.add_argument('--new-tab', action='store_true', dest='new_tab',
help=_('If Terminator is already running, just open a new tab')) help=_('If Terminator is already running, just open a new tab'))
parser.add_option('--unhide', action='store_true', dest='unhide', parser.add_argument('--unhide', action='store_true', dest='unhide',
help=_('If Terminator is already running, just unhide all hidden windows')) help=_('If Terminator is already running, just unhide all hidden windows'))
parser.add_option('--list-profiles', action='store_true', dest='list_profiles', parser.add_argument('--list-profiles', action='store_true', dest='list_profiles',
help=_('List all profiles')) help=_('List all profiles'))
parser.add_option('--list-layouts', action='store_true', dest='list_layouts', parser.add_argument('--list-layouts', action='store_true', dest='list_layouts',
help=_('List all layouts')) help=_('List all layouts'))
for item in ['--sm-client-id', '--sm-config-prefix', '--screen', '-n', for item in ['--sm-client-id', '--sm-config-prefix', '--screen', '-n',
'--no-gconf' ]: '--no-gconf' ]:
parser.add_option(item, dest='dummy', action='store', parser.add_argument(item, dest='dummy', action='store',
help=SUPPRESS_HELP) help=argparse.SUPPRESS)
global options global options
(options, args) = parser.parse_args() options = parser.parse_args()
if len(args) != 0:
parser.error('Additional unexpected arguments found: %s' % args)
if options.version: if options.version:
print('%s %s' % (version.APP_NAME, version.APP_VERSION)) print('%s %s' % (version.APP_NAME, version.APP_VERSION))
@ -178,5 +166,4 @@ icon for the window (by file or name)'))
if util.DEBUG == True: if util.DEBUG == True:
dbg('OptionParse::parse_options: command line options: %s' % options) dbg('OptionParse::parse_options: command line options: %s' % options)
return(options,optionslist) return(options,optionslist)