By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Here’s How You Can Build Your Own Markdown Parser: Part 2 – How to Read File From the Command Line | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Here’s How You Can Build Your Own Markdown Parser: Part 2 – How to Read File From the Command Line | HackerNoon
Computing

Here’s How You Can Build Your Own Markdown Parser: Part 2 – How to Read File From the Command Line | HackerNoon

News Room
Last updated: 2025/08/02 at 6:50 PM
News Room Published 2 August 2025
Share
SHARE

In this post, we will implement a command-line interface that’ll accept a markdown file as a command-line argument, read its content, and print the entire content of the file in the terminal.

After doing this, we will be well on our way to start implementing parsing basic texts, headers, lists, and so on.

I’ll explain all the code in detail, and in the end, I’ll give you the entire code so that you can try without any issues.

Required Modules

There are two modules that we need to use:

  • std::env – we need env module for interacting with the environment the program is currently running in. Now, we use it here to access the command-line arguments passed to our program.

  • std::fs – we need fs for file operations like reading from and writing to files.

    use std::env; use std::fs;

Getting the markdown file from the command line.

After getting the required modules, we can start working on getting the markdown file from the command line argument. From now on, we will write every line of code inside the main function.

let args: Vec<String> = env::args().collect();

Let’s understand what this line does.

  • env::args() – This function returns an iterator of the arguments provided in the command line to the program.
  • collect() – Then we are using the collect() method to consume the iterator and collect all the items from that iterator into a collection.
  • Vec<String> – To store all the items from the iterator, we are using the Vector collection that stores elements of type String. We gave the variable name of this Vec<String> as args. So, this args is holding all the items from that command line iterator.

Now, let’s handle the case where the user either knowingly or unknowingly skips giving the markdown file in the command line argument. In this case, we need to ask the user to provide a markdown file.

if args.len() < 2 {
		println!("Please provide a markdown file");
		return;
}

Here, we are saying if the number of commands is less than 2, then print a statement asking the user to provide a markdown file and then return from the program.

[!Note] Number of Arguments In command line, the first argument is the program’s name. We need another argument that’ll represent the filename. Hence, we need the length of the args to be at least 2.

Reading the file and printing its content.

Now, let’s store the filename that we got from the command line argument.

let filename = &args[1];

We are creating a variable filename and storing the filename from the 2nd command-line argument.

Now, let’s start reading the raw content of the file and print the entire content as it is.

println!("Attempting to read file: {}", filename);
let content = fs::read_to_string(filename).expect("Something went wrong reading the file");
println!("File content:n{}", content);
  • First, we are just printing a logging statement to know that we are attempting to read the file.
  • fs::read_to_string(filename) – This function comes from the fs module that reads the entire content of the file specified by the filename.
  • Then, we are storing the entire content in the content variable and finally printing out the entire content on the console.

Before explaining the expect() method, let’s understand a specific data type in Rust.

Result in Rust

In Rust, the way we handle operations that might fail is by using Result type. It’s an enum with two variants:

enum Result<T, E> {
    Ok(T),    // Success - contains the value of type T
    Err(E),   // Error - contains the error of type E
}

Now, let’s get back to the fs::read_to_string() function. If you look in the docs, it returns a Result type, and for a success case, it returns a String type, and for a failure case, it returns a std::io::Error type.

Now, let’s connect this with the expect() method.

This expect() method is a shortcut that says:

  • if fs::read_to_string() works fine and returns Ok(string), then return the string value.
  • if fs::read_to_string() doesn’t work, then crash the program with the custom error message (“Something went wrong reading the file”).

Testing the Program

To test, let’s create a test.md file in the root of the project and put some random markdown text.

# this is a header
- this is a list
this is a normal text

Now, open your terminal from your project directory and type the command:

cargo run -- test.md

It’ll compile and execute the code and give you the following output.

reading raw content from markdown filereading raw content from markdown file

This output shows that we can successfully get the markdown file’s content and print its raw data. In the next post, we will start to learn about parsing.

Complete Source Code

use std::env;
use std::fs;

fn main() {
    let args: Vec<String> = env::args().collect();

    if args.len() < 2 {
        println!("Please provide a markdown file");
        return;
    }

    let filename = &args[1];

    println!("Attempting to read file: {}", filename);

    let content = fs::read_to_string(filename).expect("Something went wrong reading the file");

    println!("File content:n{}", content);
}

Conclusion

In this post, we understood how to interact with the command line and also read the markdown file from the command line argument. We also understood basic error handling using Result where we know things might go wrong, and we made sure that our program can handle that.

Now, we understand reading and printing all the markdown file content. From the next post, we are slowly going to implement our parsing algorithm step by step and cover basic text, headers, lists, and other things one by one. I’m really excited for the next post; I hope you are excited too. See you soon.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Android Auto now plays nicely with your phone’s color scheme
Next Article Tim Cook Tells Employees That The AI Race Is Still Apple’s To Lose, Despite Setbacks – BGR
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Why You Shouldn’t Put An AirTag In Your Kid’s Shoes – BGR
News
5G standalone adoption accelerates mobile core network market | Computer Weekly
News
5 productivity apps I swear by, and one of them unlocks the rest
News
Marketing Project Management: Tools, Tips & Templates |
Computing

You Might also Like

Computing

Marketing Project Management: Tools, Tips & Templates |

30 Min Read
Computing

Intel QuickAssist Hit By Second Demotion In Linux 6.17 Due To Lack Of Kernel Benefit

3 Min Read
Computing

Miro Product Roadmap Templates to Align Strategy in 2025

26 Min Read
Computing

Week in Review: Most popular stories on GeekWire for the week of July 27, 2025

4 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?