We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Arguments

BookBot is done! Almost...

You've probably noticed that most CLI tools you use (ls, cd, etc.) are not limited to a single "hardcoded" input. Right now, bookbot can only analyze books/frankenstein.txt. That's not very useful.

Let's update the program so we can pass in any book path instead of using a hardcoded value.

System Arguments

Python's built-in sys module provides access to command-line arguments in the terminal. In particular, sys.argv is a list of strings representing the arguments passed to the script.

The first item in the list is the script name; the rest are the arguments. For example, python3 main.py creates a sys.argv list that looks like this:

print(sys.argv)
# Prints ['main.py']

And python3 main.py books/frankenstein.txt creates a sys.argv list that looks like this:

print(sys.argv)
# Prints ['main.py', 'books/frankenstein.txt']

print(len(sys.argv))
# Prints 2

print(sys.argv[0])
# Prints 'main.py'

print(sys.argv[1])
# Prints 'books/frankenstein.txt'

Assignment

  1. import sys
    
  2. Use the books that are linked above, or the CLI tests will fail.

    You can also run the following commands from the project root to download and create the files:

    mkdir -p books
    curl -L "https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/mobydick.txt" -o books/mobydick.txt
    curl -L "https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/prideandprejudice.txt" -o books/prideandprejudice.txt
    

Run and submit the CLI tests from the root of your project.

If the tests pass, it means you've finished building BookBot. Congrats!

I hope you enjoyed this project. It's always good to solidify the things you learn in courses by getting your hands dirty with something practical.