Reading Command-Line Arguments
In the welcome video, the JavaScript port of Dev’s first lines printed the path of digest.js
and NaN, with no error. In this lesson, you found out why, and read Dev’s two command-line arguments the
JavaScript way.
Both files are trimmed to their argument lines for now, with one line standing in for the
digest’s work. Dev runs python digest.py logs/week-44.log 75, and any reading over the limit,
75, gets flagged. Python’s sys.argv holds the words he typed after python, starting with
the script, so the week’s log is at index 1 and the limit at index 2:
import sys
def main():
path = sys.argv[1]
threshold = float(sys.argv[2])
print(f"Checking {path} against {threshold}")
if __name__ == "__main__":
main()
It prints Checking logs/week-44.log against 75.0. A line-by-line port swaps sys.argv for
process.argv and keeps the same indexes. Node’s process object is a global, so
nothing is imported. float() becomes Number(), which also turns a string into a number: