natefaubion/purescript-argparse-basic

some issues

safareli opened this issue · 7 comments

Hi, I was playing around with this lib, here is what my code looks like: https://github.com/natefaubion/purescript-argparse-basic/compare/main...safareli:playground?expand=1

But when i run the code I'm getting this output:

git
    Unexpected argument:
        

    This is my git CLI example.

    --help,-h       Show this help message.
    --version,-v    show version
    
    commit          commit changes
    stash           stash changes
_____
git
    Unexpected argument:
        commit -b=body -m=asd --amend

    This is my git CLI example.

    --help,-h       Show this help message.
    --version,-v    show version
    
    commit          commit changes
    stash           stash changes
_____
git
    Unexpected argument:
        stash --help

    This is my git CLI example.

    --help,-h       Show this help message.
    --version,-v    show version
    
    commit          commit changes
    stash           stash changes
_____
git
    Unexpected argument:
        stash push -m=bla

    This is my git CLI example.

    --help,-h       Show this help message.
    --version,-v    show version
    
    commit          commit changes
    stash           stash changes

and it seams to be wrong, see:

https://github.com/natefaubion/purescript-argparse-basic/compare/main...safareli:playground?expand=1#diff-79394319b4f162a6bbcf25d46de2eb64867c4fd2c3b5ec63fcda393198f771c1R56-R62

I might be missing something or using the lib in wrong way, not sure.

(feel free to use that branch to debug the issue or use the example if you find it useful)

parseArgs expects the input to be tokenized on spaces already, as if you were getting arguments from something like Process.argv. It does not do this tokenization on it's own.

Haha, what was I thinking, thanks!

One thing I noticed is that Applicative instance for ArgParser might be useful I have added one on my branch and it seams to work.

Closing this issue.

One thing I noticed is that Applicative instance for ArgParser might be useful

I also think this would be useful, unless I'm missing some way of constructing an ArgParser Command for some Command that takes no arguments. I've been doing

SomeCommand <$ ArgParse.fromRecord {}

That’s true. There are several ways to implement a unit parser, so omitting an Applicative instance doesn’t accomplish anything.

It’s hard for me to comment on your example without larger context, but in general I don’t see a use case for pure. I have yet to write an arg parser where it was needed, but maybe that’s just me.

It’s hard for me to comment on your example without larger context

Making a version of til, my early progress here. Maybe I just want

ArgParse.flag [ "build" ] --...

instead of

ArgParse.command [ "build" ] --...

although it seems more like a "command" than "flag" 🤷

Sorry, you might need to give me a high level idea of what you are trying to accomplish.

The thing I'm trying to make is just a very simple CLI where some commands take arguments

$ til edit Some new blog post -f media.jpg

(opens $EDITOR to edit "Some new blog post") and some don't

$ til sync

(just wraps git fetch & rebase). I don't write CLIs typically, so I had thought of a "flag" as specifically something following a -- or -, so initially skipped over the flag constructor. In any case, this is what I went with and accomplishes what I was trying to, without need for fromRecord {} 🙂

data Command
  = Build
  | Sync
  | Edit
      (Maybe { title :: String, mediaFilePaths :: Array String })

parser :: ArgParser Command
parser = ArgParse.choose "command"
  [ ArgParse.flag [ "build" ]
      "Build all documents to HTML files for distribution"
      $> Build
      <* ArgParse.flagHelp
  , ArgParse.flag [ "sync" ]
      "Ensure the local environment has all known changes"
      $> Sync
      <* ArgParse.flagHelp
  , ArgParse.command [ "edit" ]
      "Edit and publish a new document"
      do
        Edit
          <$> ArgParse.optional
            ( ArgParse.fromRecord
                { title
                , mediaFilePaths
                }
            )
          <* ArgParse.flagHelp
  ]
  where
  title = do
    let parts = ArgParse.unfolded (ArgParse.anyNotFlag "TITLE" "Title")
    String.joinWith " " <$> parts
  mediaFilePaths =
    ArgParse.argument [ "--files", "-f" ] "Paths to media files"
      # ArgParse.separated "FILE" (Pattern ",")