Package: python-nubia
python-nubiaNubia is a lightweight framework for building command-line applications with Python. It was originally designed for the “logdevice interactive shell (aka.ldshell)” at Facebook. Since then it was factored out to be a reusable component and several internal Facebook projects now rely on it as a quick and easy way to get an intuitive shell/cli application without too much boilerplate.Nubia is built on top ofpython-prompt-toolkitwhich is a fantastic toolkit for building interactive command-line applications.Disclaimer: Nubia is beta for non-ldshell use-cases. Some of the design decisions might sound odd but they fit the ldshell usecase perfectly. We are continuously making changes to make it more consistent and generic outside of the ldshell use-case. Until a fully stable release is published, use it on your own risk.See theCONTRIBUTINGfile for how to help out.If you are curious on the origins of the name, checkoutNubia on Wikipediawith its unique and colourful architecture.Key FeaturesInteractive mode that offers fish-style auto-completionCLI mode that gets generated from your functions and classes.Optional bash/zsh completions via an external utility ‘nubia-complete’ (experimental)A customisable status-bar in interactive mode.An optional IPython-based interactive shellArguments with underscores are automatically hyphenatedPython3 type annotations are used for input type validationInteractive modeThe interactive mode in Nubia is what makes it unique. It is very easy to build a unique shell for your program with zero overhead. The interactive shell in its simplistic form offers automatic completions for commands, sub-commands, arguments, and values. It also offers a great deal of control for developers to take control over  auto-completions, even for commands that do not fall under the typical format. An example is the “select” command in ldshell which is expressed as a SQL-query. We expect that most use cases of Nubia will not need such control and the AutoCommand will be enough without further customisation.If you start a nubia-based program without a command, it automatically starts an interactive shell. The interactive mode looks like this:Non-interactive modeThe CLI mode works exactly like any traditional unix-based command line utility.Have your@commanddecorated function return anintto send that value as the Unix return code for your non interactive CLI.ExamplesIt starts with a function like this:importsocketimporttypingfromtermcolorimportcprintfromnubiaimportargument,command,context@command@argument("hosts",description="Hostnames to resolve",aliases=["i"])@argument("bad_name",name="nice",description="testing")deflookup(hosts:typing.List[str],bad_name:int)->int:"""This will lookup the hostnames and print the corresponding IP addresses"""ctx=context.get_context()ifnothosts:cprint("No hosts supplied via --hosts")return1print(f"hosts:{hosts}")cprint(f"Verbose?{ctx.verbose}")forhostinhosts:cprint(f"{host}is{socket.gethostbyname(host)}")return0RequirementsNubia-based applications require python 3.6+ and works with both Mac OS X or Linux. While in theory it should work on Windows, it has never been tried.Installing NubiaIf you are installing nubia for your next project, you should be able to easily use pip for that:pip3installpython-nubiaBuilding Nubia from sourceYou can either setup.py to build a tarball, or use pipenv to setup a virtualenv with all the dependencies installed.Running example in virtualenv:It's often best to create a virtualenv to contain the dependencies required for python-nubia project.python3-mvenv.venvsource.venv/bin/activate
pipinstall-rrequirements.txtIf you would like to run the example, then you need to add the root of the source tree into your PYTHONPATH.virtualenv.venvsource.venv/bin/activateexportPYTHONPATH="$(pwd)"python3example/nubia_example.pyTo run the unit tests:From within the virtualenv you can use nosetests:nosetestsOr if you don't want to create a virtualenv, just use:python3setup.pynosetestsGetting StartedSee thegetting startedguide to learn how to build a simple application with Nubia.Licensepython-nubia is BSD licensed, as found in the LICENSE file.