Add OptionInFile and OptionOutFile with lazy wrapper objects.#93
Open
Add OptionInFile and OptionOutFile with lazy wrapper objects.#93
Conversation
Add convenience option types that return InFile/OutFile wrappers supporting lazy opening, "-" for stdin/stdout, and optional file existence checking at parse time.
floitsch
commented
Apr 2, 2026
| block.call writer | ||
| finally: | ||
| writer.close | ||
|
|
Member
Author
There was a problem hiding this comment.
Would be nice to have the same convenience method as for reading (write-contents).
| return InFile.stdin_ | ||
| if check-exists and not for-help-example: | ||
| if not file.is-file str: | ||
| throw "File not found for option '$name': '$str'." |
Member
Author
There was a problem hiding this comment.
This should be done using Ui.abort. Not sure how to get the UI class, though.
| parse str/string --for-help-example/bool=false -> any: | ||
| if allow-dash and str == "-": | ||
| return InFile.stdin_ | ||
| if check-exists and not for-help-example: |
Member
Author
There was a problem hiding this comment.
The check should be a method on the InFile. If we don't automatically check here, then we should have the option of doing it later by simply calling check.
Same for the OutFile.
| expect-not option.check-exists | ||
|
|
||
| // Test parse returns InFile for a path. | ||
| in-file := (option.parse "/some/path") as cli.InFile |
Member
Author
There was a problem hiding this comment.
Don't use as when the RHS is typed as any.
Use:
in-file/cli.InFile := option.parse "/some/path"
Here and for the rest of the file.
| expect-not out-file.is-stdout | ||
|
|
||
| // Test writing to a real file. | ||
| tmpdir := directory.mkdtemp "/tmp/cli-test-" |
Member
Author
There was a problem hiding this comment.
Use with-tmp-directory (like many other tests). Here and above.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add convenience option types that return InFile/OutFile wrappers supporting lazy opening, "-" for stdin/stdout, and optional file existence checking at parse time.