コード例 #1
0
ファイル: helper.py プロジェクト: Dieterbe/ranger
def parse_arguments():
	"""Parse the program arguments"""
	from optparse import OptionParser, SUPPRESS_HELP
	from ranger import __version__
	from ranger.ext.openstruct import OpenStruct
	from os.path import expanduser

	if 'XDG_CONFIG_HOME' in os.environ and os.environ['XDG_CONFIG_HOME']:
		default_confdir = os.environ['XDG_CONFIG_HOME'] + '/ranger'
	else:
		default_confdir = '~/.config/ranger'
	usage = '%prog [options] [path/filename]'

	minor_version = __version__[2:]  # assumes major version number is <10
	if '.' in minor_version:
		minor_version = minor_version[:minor_version.find('.')]
	version_tag = ' (stable)' if int(minor_version) % 2 == 0 else ' (testing)'
	if __version__.endswith('.0'):
		version_string = 'ranger ' + __version__[:-2] + version_tag
	else:
		version_string = 'ranger ' + __version__ + version_tag

	parser = OptionParser(usage=usage, version=version_string)

	parser.add_option('-d', '--debug', action='store_true',
			help="activate debug mode")
	parser.add_option('-c', '--clean', action='store_true',
			help="don't touch/require any config files. ")
	parser.add_option('--fail-if-run', action='store_true', # COMPAT
			help=SUPPRESS_HELP)
	parser.add_option('--copy-config', type='string', metavar='which',
			help="copy the default configs to the local config directory. "
			"Possible values: all, apps, commands, keys, options, scope")
	parser.add_option('--fail-unless-cd', action='store_true',
			help="experimental: return the exit code 1 if ranger is" \
					"used to run a file (with `ranger filename`)")
	parser.add_option('-r', '--confdir', type='string',
			metavar='dir', default=default_confdir,
			help="the configuration directory. (%default)")
	parser.add_option('-m', '--mode', type='int', default=0, metavar='n',
			help="if a filename is supplied, run it with this mode")
	parser.add_option('-f', '--flags', type='string', default='',
			metavar='string',
			help="if a filename is supplied, run it with these flags.")
	parser.add_option('--choosefile', type='string', metavar='TARGET',
			help="Makes ranger act like a file chooser. When opening "
			"a file, it will quit and write the name of the selected "
			"file to TARGET.")
	parser.add_option('--choosedir', type='string', metavar='TARGET',
			help="Makes ranger act like a directory chooser. When ranger quits"
			", it will write the name of the last visited directory to TARGET")

	options, positional = parser.parse_args()
	arg = OpenStruct(options.__dict__, targets=positional)
	arg.confdir = expanduser(arg.confdir)
	if arg.fail_if_run:
		arg.fail_unless_cd = arg.fail_if_run
		del arg['fail_if_run']

	return arg
コード例 #2
0
def parse_arguments():
    """Parse the program arguments"""
    from optparse import OptionParser, SUPPRESS_HELP
    from ranger import __version__
    from ranger.ext.openstruct import OpenStruct
    from os.path import expanduser

    if 'XDG_CONFIG_HOME' in os.environ and os.environ['XDG_CONFIG_HOME']:
        default_confdir = os.environ['XDG_CONFIG_HOME'] + '/ranger'
    else:
        default_confdir = '~/.config/ranger'
    usage = '%prog [options] [path/filename]'

    minor_version = __version__[2:]  # assumes major version number is <10
    if '.' in minor_version:
        minor_version = minor_version[:minor_version.find('.')]
    version_tag = ' (stable)' if int(minor_version) % 2 == 0 else ' (testing)'
    if __version__.endswith('.0'):
        version_string = 'ranger ' + __version__[:-2] + version_tag
    else:
        version_string = 'ranger ' + __version__ + version_tag

    parser = OptionParser(usage=usage, version=version_string)

    parser.add_option('-d',
                      '--debug',
                      action='store_true',
                      help="activate debug mode")
    parser.add_option('-c',
                      '--clean',
                      action='store_true',
                      help="don't touch/require any config files. ")
    parser.add_option(
        '--fail-if-run',
        action='store_true',  # COMPAT
        help=SUPPRESS_HELP)
    parser.add_option(
        '--copy-config',
        type='string',
        metavar='which',
        help="copy the default configs to the local config directory. "
        "Possible values: all, apps, commands, keys, options, scope")
    parser.add_option('--fail-unless-cd', action='store_true',
      help="experimental: return the exit code 1 if ranger is" \
        "used to run a file (with `ranger filename`)")
    parser.add_option('-r',
                      '--confdir',
                      type='string',
                      metavar='dir',
                      default=default_confdir,
                      help="the configuration directory. (%default)")
    parser.add_option('-m',
                      '--mode',
                      type='int',
                      default=0,
                      metavar='n',
                      help="if a filename is supplied, run it with this mode")
    parser.add_option(
        '-f',
        '--flags',
        type='string',
        default='',
        metavar='string',
        help="if a filename is supplied, run it with these flags.")
    parser.add_option(
        '--choosefile',
        type='string',
        metavar='TARGET',
        help="Makes ranger act like a file chooser. When opening "
        "a file, it will quit and write the name of the selected "
        "file to TARGET.")
    parser.add_option(
        '--choosedir',
        type='string',
        metavar='TARGET',
        help="Makes ranger act like a directory chooser. When ranger quits"
        ", it will write the name of the last visited directory to TARGET")

    options, positional = parser.parse_args()
    arg = OpenStruct(options.__dict__, targets=positional)
    arg.confdir = expanduser(arg.confdir)
    if arg.fail_if_run:
        arg.fail_unless_cd = arg.fail_if_run
        del arg['fail_if_run']

    return arg