mplewis / csvtomd

📝📊 Convert your CSV files into Markdown tables.
MIT License
655 stars 90 forks source link

Allow reading from stdin. #16

Closed Prillan closed 7 years ago

Prillan commented 7 years ago

This makes it possible to do things like:

$ csvtomd < test/input/normal.csv 
First Name  |  Last Name  |  Location           |  Allegiance
------------|-------------|---------------------|-----------------
Mance       |  Rayder     |  North of the Wall  |  Wildlings
Margaery    |  Tyrell     |  The Reach          |  House Tyrell
Danerys     |  Targaryen  |  Meereen            |  House Targaryen
Tyrion      |  Lannister  |  King's Landing     |  House Lannister

Or perhaps

$ xclip -o | csvtomd | xclip

The argument parser is modified to accept zero or more arguments with stdin ('-') as default. Thus the following are equivalent, csvtomd and csvtomd -. Using any other argument together with - results in an error.

$ csvtomd - -
Standard input can only be used alone.
mplewis commented 7 years ago

Hey! Sorry, I didn't realize this PR was open. I will take a look at it this week.

mplewis commented 7 years ago

I like this feature. I'll take a look and see if there is any obvious way to add tests for a command line feature.

Jab2870 commented 7 years ago

This would definitely be helpful to me

Prillan commented 7 years ago

@mplewis The easiest would probably by to send the arguments array and the output stream as arguments to main, like this for the normal case main(sys.argv, sys.stdout) and then something that you can control for the test cases. For output, change print(...) to print(..., file=output).

I've never tried this myself and I don't know if it's the best way of doing it, but it works.

mplewis commented 7 years ago

Thanks @Prillan – this looks good to me. I put together a shell script to test stdin functionality. I'll land that after merging this.