python 3.12 match statement makes it really easy to write ad-hoc option parsers with state machines
Conversation
Notices
-
Embed this notice
✧✦Catherine✦✧ (whitequark@mastodon.social)'s status on Monday, 21-Apr-2025 08:42:31 JST ✧✦Catherine✦✧
- clacke and clacke at libranet.de is my main like this.
-
Embed this notice
✧✦Catherine✦✧ (whitequark@mastodon.social)'s status on Monday, 21-Apr-2025 08:50:11 JST ✧✦Catherine✦✧
yes yes i fucked up. this needs a while loop or something
-
Embed this notice
Helge Heß (helge@mastodon.social)'s status on Monday, 21-Apr-2025 08:58:44 JST Helge Heß
@whitequark Looks like Swift, except in Swift, you rather link a 6MB big argument-parser lib that statically types out all the possible options 🙈
(but really, the `match` looks like how Swift's `switch` works) -
Embed this notice
✧✦Catherine✦✧ (whitequark@mastodon.social)'s status on Monday, 21-Apr-2025 09:06:00 JST ✧✦Catherine✦✧
@simon ... excellent idea
-
Embed this notice
Simon Sapin (simon@tutut.delire.party)'s status on Monday, 21-Apr-2025 09:06:02 JST Simon Sapin
@whitequark or name the iterator and call next() on it for --triple?
-
Embed this notice
✧✦Catherine✦✧ (whitequark@mastodon.social)'s status on Monday, 21-Apr-2025 09:06:15 JST ✧✦Catherine✦✧
@simon actually no that won't work because you need lookahead
-
Embed this notice
✧✦Catherine✦✧ (whitequark@mastodon.social)'s status on Monday, 21-Apr-2025 09:06:56 JST ✧✦Catherine✦✧
@helge or Rust! yeah
-
Embed this notice
poleguy (poleguy@mastodon.social)'s status on Monday, 21-Apr-2025 11:52:17 JST poleguy
@whitequark I just use typer. Good enough.
-
Embed this notice
dram🎀 (dramforever@mastodon.social)'s status on Sunday, 27-Apr-2025 02:19:18 JST dram🎀
@whitequark i had an idea... and yup
while True:
match args:
case [ '--foo', foo, *args ]:
print('foo', foo)
case [ '--baz', *args ]:
print('baz')
case [ '--', *args ]:
break
case [ arg, *args ] if arg.startswith('-'):
print('Unrecognized', arg)
raise RuntimeError() # FIXME# This can't be case _:
# Otherwise previous case overwrites args
case args:
breakclacke likes this. -
Embed this notice
✧✦Catherine✦✧ (whitequark@mastodon.social)'s status on Sunday, 27-Apr-2025 05:37:18 JST ✧✦Catherine✦✧
@dramforever oh this is cute
-
Embed this notice
dram🎀 (dramforever@mastodon.social)'s status on Sunday, 27-Apr-2025 05:37:19 JST dram🎀
@whitequark now i wonder if you can subclass list to make it not quadratic
-
Embed this notice
Simon Sapin (simon@tutut.delire.party)'s status on Sunday, 27-Apr-2025 16:00:12 JST Simon Sapin
@whitequark wow i didn’t know modern python could be this nice
(shouldn’t `--triple something` consume two arguments though?)clacke likes this. -
Embed this notice
Thomas Guyot-Sionnest (dermoth@noc.social)'s status on Sunday, 27-Apr-2025 16:00:21 JST Thomas Guyot-Sionnest
@whitequark What I really like is matching directly object or property types in the case statements. ex:
```python
match fruit_obj:
case Apple(color=Red()):
print("I'm a red apple")
case Apple():
print("I'm an apple")
case Orange():
print("I'm an orange")
case _:
print(f"Unknown fruit type: {type(fruit_obj)}")
```P.S. match case statements were introduced in 3.10.
clacke likes this.