o/ Rustlers,

So, as an indirect result of the absence of pattern matching on unique
vectors, I'm having a lot of trouble writing a sane CLI interface based on
the results of os::args()

The full code in a more readable format is available at here[1]

So what I really want is something like:

   let args = os::args();
   match args.as_slice() {
       [] => unreachable!(),
       [ref argv0] => println!("Called as: {}", argv0),
       [ref argv0, "foo", ref argv2] => println!("Matched on foo"),
       [ref argv0, "bar", ref argv2] => println!("Matched on bar"),
       [ref argv0, ref argv1, ref argv2] => println!("Called as: {} with: {}, 
{}", argv0, argv1, argv2),
       _ => fail!("OHSHI-"),
   }

Basically, I'm interested in matching my CLI on some subcommands, with a
variable number of arguments.

I got this *really close* to working with something closer to:

   let args = os::args();
   match args.as_slice() {
       [] => unreachable!(),
       [ref argv0] => println!("Called as: {}", argv0),
       [ref argv0, ref argv1] => println!("Called as: {} with: {}", argv0, 
argv1),
       [ref argv0, ref argv1, ref argv2] => {
           println!("Called as: {} with: {}, {}", argv0, argv1, argv2);
           match argv1 {
               "foo" => println!("Matched on foo"),
               "bar" => println!("Matched on bar"),
               _ => println!("No match on `argv1`")
           }
       },
       _ => fail!("OHSHI-"),
   }

But this still gets very upset because of trying to match a ~str against a
&'static str

My resulting two questions are:

While I understand the virtues of memory/type safety, it seems that (naively)
for the purposes of a string comparison to pattern match on, matching a
string literal against a string variable that's in scope should more or less
Just Work from the user perspective,

I'm not asking for this to be solved at the language level, despite how nice
that would be for people just picking this up, in the short term my
assumption is that I'll wind up pulling this out into a module or library,
how *should* I be attempting to do something like this? Is it actually
supported? I've been working on rust-http over the last few days and I
understand enough to see how I could build out macros to do what I want, but
it seems like a lot of error prone indirection that isn't strictly necessary.


Cheers

richo

[1]: https://gist.github.com/richo/11104624

--
richo

Attachment: pgpX_ZpS0foBt.pgp
Description: PGP signature

_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to