Code Monkey home page Code Monkey logo

flag-parsing's Introduction

THIS IS DEPRECATED AND WILL BE REMOVED WHEN RAPPTZ WORKS ON AN OFFICIAL IMPLEMENTATION

version python-s license-s

โš  Please be sure to read the entire README, it explains some important tricks.

Flag Parsing

A util for discord.py bots that allow passing flags into commands.

To install, run the following command:

pip install discord-flags

You can install the legacy parser by running:

pip install discord-flags==1.5.2

2.1.0 changes how signatures appear. If you wish to use the legacy signatures, use command.old_signature instead.

Basic example usage:

import discord
from discord.ext import flags, commands

bot = commands.Bot("!")

# Invocation: !flags --count=5 --string "hello world" --user Xua --thing y

@flags.add_flag("--count", type=int, default=10)
@flags.add_flag("--string", default="hello!")
@flags.add_flag("--user", type=discord.User)
@flags.add_flag("--thing", type=bool)
@flags.command()
async def flags(ctx, **flags):
    await ctx.send("--count={count!r}, --string={string!r}, --user={user!r}, --thing={thing!r}".format(**flags))
bot.add_command(flags)

Important note that @flags.command MUST be under all @flags.add_flag decorators.

@flags.add_flag takes the same arguments as argparse.ArgumentParser.add_argument to keep things simple.

Subcommands are just as simple:

@commands.group()
async def my_group(ctx):
    ...

@flags.add_flag("-n")
@my_group.command(cls=flags.FlagCommand)
async def my_subcommand(ctx, **flags):
    ...

Usage of discord.py's consume rest behaviour is not perfect with discord-flags, meaning that you have to use a flag workaround:

@flags.add_flag("message", nargs="+")
@flags.command()
async def my_command(ctx, arg1, **options):
    """ You can now access `message` via `options['message']` """
    message = ' '.join(options['message'])

flag-parsing's People

Contributors

interstella0 avatar pendragonlore avatar xuathegrate avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.