summaryrefslogtreecommitdiff
path: root/animism-align/cli/app/utils/click_utils.py
blob: 83e4f75191116faef40c0e1264d69895578ef16b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Custom Click parameter types
"""
import click

from app.settings import app_cfg
from app.models import types

# --------------------------------------------------------
# Click command helpers
# --------------------------------------------------------

def enum_to_names(enum_type):
  return {x.name.lower(): x for x in enum_type}
  
def show_help(enum_type):
  names = enum_to_names(enum_type)
  return 'Options: "{}"'.format(', '.join(list(names.keys())))

def get_default(opt):
  return opt.name.lower()


# --------------------------------------------------------
# Custom Click parameter class
# --------------------------------------------------------

class ParamVar(click.ParamType):

  name = 'default_type'

  def __init__(self, param_type):
    self.opts = {x.name.lower(): x for x in param_type}
  
  def convert(self, value, param, ctx):
    """converts (str) repr to Enum hash"""
    try:
      return self.opts[value.lower()]
    except:
      self.fail('{} is not a valid option'.format(value, param, ctx))