Let’s start with the simplest example to get familiar with SpaceWorld.
from spaceworld import run
@run
def main():
print("Hello World")
Copy this code into a file called main.py and run it:
$ python main.py
Hello World
$ python main.py --help
Usage: main [OPTIONS]
Activated modes: All
Now let’s make our command more useful by adding a name parameter:
from spaceworld import run
@run
def hello(name: str):
print(f"Hello {name}")
When we run this, something interesting happens:
$ python main.py hello
ERROR: Missing required argument: 'name'
SpaceWorld tells us exactly what’s missing! Let’s provide the name:
$ python main.py hello bino
Hello bino
Great! The command now greets us personally. But what if we want the greeting in uppercase? Let’s add a flag for that.
Flags are special command-line arguments that start with --. Let’s add an upper flag to control capitalization:
from spaceworld import run
@run
def hello(name: str, upper: bool):
if upper:
print(f"HELLO {name.upper()}")
else:
print(f"Hello {name}")
Now let’s test it:
$ python main.py hello bino
ERROR: Missing required argument: 'upper'
$ python main.py hello bino --upper
HELLO BINO
Perfect! The --upper flag converted our greeting to uppercase.
There are two main types of flags in SpaceWorld:
--upper and --help)
TrueFalse--flagname--name=value--count=5 or --file=data.txtLet’s expand our hello command with more options:
from spaceworld import run
@run
def hello(name: str, age: int, upper: bool, informal: bool = False):
greeting = "Hi" if informal else "Hello"
message = f"{greeting} {name}! You are {age} years old!"
print(message.upper() if upper else message)
Now we can use it in different ways:
$ python main.py hello bino 23 --upper
HELLO BINO! YOU ARE 23 YEARS OLD!
$ python main.py hello bino 23 --informal
Hi bino! You are 23 years old!
$ python main.py hello bino 23 --upper --informal
HI BINO! YOU ARE 23 YEARS OLD!
One of SpaceWorld’s nice features is that flag order doesn’t matter. These all work the same:
$ python main.py hello bino 23 --upper --informal
$ python main.py hello bino 23 --informal --upper
$ python main.py hello --upper bino --informal 23
However, positional argument order does matter. Since SpaceWorld doesn’t know which positional argument is which, they’re passed in the order you provide them:
# This works: name="bino", age=23
$ python main.py hello bino 23 --upper
# This fails: name="23", age="bino" (can't convert "bino" to integer)
$ python main.py hello 23 bino --upper
ERROR: Invalid argument for 'age': invalid literal for int() with base 10: 'bino'