Skip to content

Commit

Permalink
[options api] add vd.optalias and resolve aliases when setting
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Jun 29, 2023
1 parent 9cd989c commit ce497f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
3 changes: 3 additions & 0 deletions visidata/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,7 @@ def g(obj, v):
return g


vd.optalias('force_valid_colnames', 'clean_names')


vd.addGlobals(globals())
36 changes: 15 additions & 21 deletions visidata/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,18 @@ def duptty():

return stdin, stdout

option_aliases = {}
def optalias(abbr, name, val=None):
option_aliases[abbr] = (name, val)


optalias('i', 'interactive')
optalias('N', 'nothing')
optalias('f', 'filetype')
optalias('p', 'play')
optalias('b', 'batch')
optalias('P', 'preplay')
optalias('y', 'confirm_overwrite', False)
optalias('o', 'output')
optalias('w', 'replay_wait')
optalias('d', 'delimiter')
optalias('c', 'config')
optalias('r', 'dir_recurse')
optalias('force_valid_colnames', 'clean_names') # deprecated
vd.optalias('i', 'interactive')
vd.optalias('N', 'nothing')
vd.optalias('f', 'filetype')
vd.optalias('p', 'play')
vd.optalias('b', 'batch')
vd.optalias('P', 'preplay')
vd.optalias('y', 'confirm_overwrite', False)
vd.optalias('o', 'output')
vd.optalias('w', 'replay_wait')
vd.optalias('d', 'delimiter')
vd.optalias('c', 'config')
vd.optalias('r', 'dir_recurse')


@visidata.VisiData.api
Expand Down Expand Up @@ -226,10 +220,10 @@ def main_vd():
pass

optname = optname.replace('-', '_')
optname, optval = option_aliases.get(optname, (optname, optval))
optname, optval = vd._resolve_optalias(optname, optval)

if optval is None:
opt = options._get(optname)
if optval is None: # missing argument, maybe bool?
opt = vd.options._get(optname)
if opt:
if type(opt.value) is bool:
optval = True
Expand Down
19 changes: 19 additions & 0 deletions visidata/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ def _get(self, k, obj=None):
return opt

def _set(self, k, v, obj=None, helpstr='', module=None):
k, v = vd._resolve_optalias(k, v) # to set deprecated and abbreviated options

opt = self._get(k) or Option(k, v, '', module)
self._cache.clear() # invalidate entire cache on any change
return self._opts.set(k, Option(k, v, opt.helpstr or helpstr, opt.module or module), obj)
Expand Down Expand Up @@ -251,6 +253,23 @@ def __setitem__(self, optname, value): # options[optname] = value
vd._options = SettingsMgr()

vd.options = vd.OptionsObject(vd._options) # global option settings
vd.option_aliases = {}


@VisiData.api
def optalias(vd, altname, optname, val=None):
'Create an alias `altname` for option `optname`, setting the value to a particular `val` (if not None).'
vd.option_aliases[altname] = (optname, val)


@VisiData.api
def _resolve_optalias(vd, optname, optval):
while optname in vd.option_aliases:
optname, v = vd.option_aliases.get(optname)
if v is not None: # value might be given
optval = v

return optname, optval


@VisiData.api
Expand Down

0 comments on commit ce497f4

Please sign in to comment.