Python Click CLI: Validate and Modify Arguments

Jul 29, 2019

Click arguments validation and modify arguments.

  • Symbol must be BTCUSDT, ETHUSDT, BTCETH as per this rule.
  • Split symbol from BTCUSDT into BTC and USDT.
import clickimport redef validate_symbol(ctx, param, value):    m = re.search(r'^(\w+)(BTC|ETH|BNB|USDT|PAX|TUSD|USDC)$', value)    if m:        c1_symbol = m.group(1)        c2_symbol = m.group(2)        return (c1_symbol, c2_symbol)    else:        raise click.BadParameter('Invalid symbol')@cli.command()@click.option('--symbol', callback=validate_symbol, required=True)def get_price(symbol):    c1_symbol, c2_symbol = symbol    symbol = ''.join(symbol)

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.