Skip to content

Add OptionInFile and OptionOutFile with lazy wrapper objects.#93

Open
floitsch wants to merge 1 commit intomainfrom
floitsch/OptionXFile
Open

Add OptionInFile and OptionOutFile with lazy wrapper objects.#93
floitsch wants to merge 1 commit intomainfrom
floitsch/OptionXFile

Conversation

@floitsch
Copy link
Copy Markdown
Member

@floitsch floitsch commented Apr 2, 2026

Add convenience option types that return InFile/OutFile wrappers supporting lazy opening, "-" for stdin/stdout, and optional file existence checking at parse time.

Add convenience option types that return InFile/OutFile wrappers
supporting lazy opening, "-" for stdin/stdout, and optional
file existence checking at parse time.
@floitsch floitsch requested a review from kasperl April 2, 2026 18:24
block.call writer
finally:
writer.close

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'."
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use with-tmp-directory (like many other tests). Here and above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant