From bbed62c336d279bec821a1fa802b6fb9a563ddad Mon Sep 17 00:00:00 2001 From: thititongumpun Date: Wed, 4 Oct 2023 23:45:28 +0700 Subject: [PATCH] feat: improved command-line to parser for automatically generates help and usage messages --- README.md | 7 +++++++ main.py | 17 +++++++++++------ requirements.txt | 12 +++++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3ae91db..42f4433 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,13 @@ Python 2.7 is already installed on most of the Linux distro. 1. Render the HTML on CLI using pandoc. 2. Get some source website to search for commands. +## Usage + +- Search linux command +```shell +python main.py -s wikipedia -q pwd +``` + ### EXAMPLE *Video and screenshot will be added soon.* diff --git a/main.py b/main.py index 15f9d40..a8e5aa1 100644 --- a/main.py +++ b/main.py @@ -1,18 +1,23 @@ import requests +import argparse from bs4 import BeautifulSoup import os import textwrap import sys import wikipedia +sources = ['wikipedia', 'wiki', 'google', 'stackoverflow'] -query = str(sys.argv[2]) -site = str(sys.argv[1]) -print("Want to know about the command : " + query) -print("Fetching information for the command "+query+" from "+site+".") -if(site == "wikipedia" or site == "wiki"): +parser = argparse.ArgumentParser() +parser.add_argument("-s", "--site", default="wikipedia", choices=sources) +parser.add_argument("-q", "--query", default="", required=True, help="command to search") + +args = parser.parse_args() +print("Want to know about the command : " + args.query) +print("Fetching information for the command "+args.query+" from "+args.site+".") +if(args.site == "wikipedia" or args.site == "wiki"): - result = wikipedia.summary("'%s' linux command" %query, sentences = 7) + result = wikipedia.summary("'%s' linux command" %args.query, sentences = 7) print(result) # soup=BeautifulSoup(html_doc, 'html.parser') diff --git a/requirements.txt b/requirements.txt index 2a7b609..8322078 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,9 @@ -bs4 -requests -wikipedia +beautifulsoup4==4.12.2 +bs4==0.0.1 +certifi==2023.7.22 +charset-normalizer==3.3.0 +idna==3.4 +requests==2.31.0 +soupsieve==2.5 +urllib3==2.0.6 +wikipedia==1.4.0