I made a mistake in the previous code, it does not print the unknown command. 
Both

> [ swap drop "Unknown command: " swap append print nl flush help flush 1 exit ]

and

> [ "Unknown command: " prepend print nl flush help flush 1 exit ]

prints it[1], although I am pretty sure there is a better way to achieve this. 
In fact, you could use "unclip" in other parts of the code, but this was just 
supposed to be a simple demonstration.

Additionally, you could create a helper word for "unknown command" if it looks 
not-so-desirable.

[1] Example output:

zolk3ri@void foo $ ./foo.out
Usage: foo [command]
Available commands:
  greet   - Print a greeting
  help    - Show this message
zolk3ri@void foo $ ./foo.out greet
Hello from Factor!
zolk3ri@void foo $ ./foo.out foo
Unknown command: foo

Usage: foo [command]
Available commands:
  greet   - Print a greeting
  help    - Show this message

Is this close to what you are trying to achieve?

On Sunday, July 6th, 2025 at 4:08 PM, Zoltán Kéri <zolk...@protonmail.com> 
wrote:

> I believe you are looking for something like this unless I am mistaken:
>
> USING: combinators command-line io kernel namespaces sequences ;
> IN: foo
>
> ! Help text
> : help ( -- )
> "Usage: foo [command]" print
> "Available commands:" print
> " greet - Print a greeting" print
> " help - Show this message" print ;
>
> ! Command: greet
> : greet ( -- )
> "Hello from Factor!" print ;
>
> ! Dispatch function: handles command and its arguments
> : dispatch ( cmd rest -- )
> {
> { "greet" [ drop greet ] }
> { "help" [ drop help ] }
> [ drop "Unknown command: " swap append print help ]
> } case ;
>
> ! Entry point
> : run ( -- )
> command-line get dup empty? [
> drop help
> ] [
> dup rest swap first dispatch
> ] if ;
>
> MAIN: run
>
> You can extend it to your liking.
>
> On Saturday, July 5th, 2025 at 9:34 PM, toastal toas...@posteo.net wrote:
>
> > > Are you trying to use something like a “sub command” feature?
> >
> > Yep. Like Cmdliner.
> >
> > https://ocaml.github.io/odoc/cmdliner/cmdliner/Cmdliner/Cmd/index.html
> >
> > The ability to call ./my-factor-script cmd sub-cmd --help.
> >
> > _______________________________________________
> > Factor-talk mailing list
> > Factor-talk@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/factor-talk


_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to