Skip to content

Commit 6055017

Browse files
martin-majlisMartin Majlisgithub-advanced-security[bot]
authored
feat: Add command line tool (#402)
* feat: Add command line tool * trivial: Reformat source code * Potential fix for code scanning alert no. 102: Workflow does not contain permissions Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * trivial: Skip empty recorded files * trivial: Add .dockerignore --------- Co-authored-by: Martin Majlis <martin.majlis+github@gmail.com> Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 84a17a3 commit 6055017

56 files changed

Lines changed: 1333 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# CLI integration test error logs
2+
tests/cli/expected/*.error

.github/workflows/cli-test.yml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: CLI Integration Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
schedule:
11+
# Run daily at 06:00 UTC
12+
- cron: "0 6 * * *"
13+
workflow_dispatch:
14+
inputs:
15+
mode:
16+
description: "Test mode: 'verify' to check against fixtures, 'record' to update fixtures"
17+
required: true
18+
default: "record"
19+
type: choice
20+
options:
21+
- verify
22+
- record
23+
24+
jobs:
25+
cli-verify:
26+
name: CLI integration tests (verify)
27+
if: github.event_name != 'workflow_dispatch' || inputs.mode == 'verify'
28+
runs-on: ubuntu-latest
29+
permissions:
30+
contents: read
31+
steps:
32+
- uses: actions/checkout@v6.0.2
33+
34+
- name: Set up Python
35+
uses: actions/setup-python@v5
36+
with:
37+
python-version: "3.11"
38+
cache: "pip"
39+
40+
- name: Install dependencies
41+
run: |
42+
python -m pip install -U pip
43+
make requirements
44+
45+
- name: Install package
46+
run: |
47+
pip install -e .
48+
49+
- name: Verify CLI output against fixtures
50+
run: |
51+
bash tests/cli/test_cli.sh verify
52+
53+
cli-record:
54+
name: CLI integration tests (record)
55+
if: github.event_name == 'workflow_dispatch' && inputs.mode == 'record'
56+
runs-on: ubuntu-latest
57+
permissions:
58+
contents: write
59+
steps:
60+
- uses: actions/checkout@v6.0.2
61+
62+
- name: Set up Python
63+
uses: actions/setup-python@v5
64+
with:
65+
python-version: "3.11"
66+
cache: "pip"
67+
68+
- name: Install dependencies
69+
run: |
70+
python -m pip install -U pip
71+
make requirements
72+
73+
- name: Install package
74+
run: |
75+
pip install -e .
76+
77+
- name: Record CLI fixtures
78+
run: |
79+
bash tests/cli/test_cli.sh record
80+
81+
- name: Verify freshly recorded fixtures
82+
run: |
83+
bash tests/cli/test_cli.sh verify
84+
85+
- name: Commit updated fixtures
86+
run: |
87+
git config user.name "github-actions[bot]"
88+
git config user.email "github-actions[bot]@users.noreply.github.com"
89+
git add tests/cli/expected/
90+
if git diff --cached --quiet; then
91+
echo "No fixture changes detected."
92+
else
93+
git commit -m "Update CLI test fixtures [automated]"
94+
git push
95+
fi

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,6 @@ modules.xml
220220

221221

222222
# End of https://www.gitignore.io/api/pycharm+all,jetbrains+all
223+
224+
# CLI integration test error logs
225+
tests/cli/expected/*.error

CLI.rst

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
CLI
2+
===
3+
4+
Wikipedia-API includes a command line tool ``wikipedia-api`` that is
5+
automatically installed with the package. It provides access to all
6+
library features from the terminal.
7+
8+
Global Options
9+
--------------
10+
11+
Every command supports the following options:
12+
13+
* ``-l, --language`` — Language edition of Wikipedia (default: ``en``)
14+
* ``-u, --user-agent`` — HTTP User-Agent string
15+
* ``-v, --variant`` — Language variant (e.g. ``zh-cn``, ``zh-tw``)
16+
* ``-f, --extract-format`` — Extraction format: ``wiki`` or ``html`` (default: ``wiki``)
17+
* ``-n, --namespace`` — Wikipedia namespace number (default: ``0`` = Main)
18+
* ``-h, --help`` — Show help for any command
19+
20+
Commands that return lists also support:
21+
22+
* ``--json`` — Output results as JSON
23+
24+
Getting Help
25+
------------
26+
27+
Show all available commands::
28+
29+
wikipedia-api --help
30+
31+
Show help for a specific command::
32+
33+
wikipedia-api summary --help
34+
35+
Show version::
36+
37+
wikipedia-api --version
38+
39+
Page Info
40+
---------
41+
42+
Show metadata about a page::
43+
44+
wikipedia-api page "Python (programming language)"
45+
46+
Output as JSON::
47+
48+
wikipedia-api page "Python (programming language)" --json
49+
50+
Summary
51+
-------
52+
53+
Print the summary of a Wikipedia page::
54+
55+
wikipedia-api summary "Python (programming language)"
56+
57+
Summary in Czech::
58+
59+
wikipedia-api summary "Ostrava" --language cs
60+
61+
Summary in Chinese with variant::
62+
63+
wikipedia-api summary "Python" --language zh --variant zh-cn
64+
65+
Full Text
66+
---------
67+
68+
Print the full text (summary + all sections)::
69+
70+
wikipedia-api text "Python (programming language)"
71+
72+
Full text in HTML format::
73+
74+
wikipedia-api text "Ostrava" --language cs --extract-format html
75+
76+
Sections
77+
--------
78+
79+
List all sections of a page::
80+
81+
wikipedia-api sections "Python (programming language)"
82+
83+
List sections as JSON::
84+
85+
wikipedia-api sections "Python (programming language)" --json
86+
87+
Print the text of a specific section::
88+
89+
wikipedia-api section "Python (programming language)" "Features and philosophy"
90+
91+
Links
92+
-----
93+
94+
List all pages linked from a page::
95+
96+
wikipedia-api links "Python (programming language)"
97+
98+
Output as JSON::
99+
100+
wikipedia-api links "Python (programming language)" --json
101+
102+
Backlinks
103+
---------
104+
105+
List pages that link to a given page::
106+
107+
wikipedia-api backlinks "Python (programming language)"
108+
109+
Output as JSON::
110+
111+
wikipedia-api backlinks "Python (programming language)" --json
112+
113+
Language Links
114+
--------------
115+
116+
List translations of a page in other language editions::
117+
118+
wikipedia-api langlinks "Python (programming language)"
119+
120+
Output as JSON::
121+
122+
wikipedia-api langlinks "Python (programming language)" --json
123+
124+
Categories
125+
----------
126+
127+
List categories a page belongs to::
128+
129+
wikipedia-api categories "Python (programming language)"
130+
131+
Output as JSON::
132+
133+
wikipedia-api categories "Python (programming language)" --json
134+
135+
Category Members
136+
----------------
137+
138+
List pages in a category::
139+
140+
wikipedia-api categorymembers "Category:Physics"
141+
142+
Recursively list subcategory members (depth 1)::
143+
144+
wikipedia-api categorymembers "Category:Physics" --max-level 1
145+
146+
Output as JSON::
147+
148+
wikipedia-api categorymembers "Category:Physics" --json
149+
150+
Complete Workflow Example
151+
-------------------------
152+
153+
Fetch a page summary, then explore its sections and links::
154+
155+
# Get summary
156+
wikipedia-api summary "Python (programming language)"
157+
158+
# List sections
159+
wikipedia-api sections "Python (programming language)"
160+
161+
# Read a specific section
162+
wikipedia-api section "Python (programming language)" "History"
163+
164+
# List categories
165+
wikipedia-api categories "Python (programming language)"
166+
167+
# Find the page in other languages
168+
wikipedia-api langlinks "Python (programming language)"
169+
170+
# Get the same page in German
171+
wikipedia-api summary "Python (Programmiersprache)" --language de

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ include CHANGES.rst
33
include README.rst
44
include DEVELOPMENT.rst
55
include API.rst
6+
include CLI.rst
67
recursive-include wikipedia *.py

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
click==8.1.8
12
requests==2.32.5

setup.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def fix_doc(txt):
2121

2222
requires = [
2323
"requests",
24+
"click",
2425
]
2526

2627
tests_require = [] # type: List[str]
@@ -57,6 +58,11 @@ def fix_doc(txt):
5758
packages=["wikipediaapi"],
5859
include_package_data=True,
5960
zip_safe=False,
61+
entry_points={
62+
"console_scripts": [
63+
"wikipedia-api=wikipediaapi.cli:main",
64+
],
65+
},
6066
extras_require={
6167
"testing": tests_require,
6268
},

tests/cli/expected/backlinks_en.txt

Whitespace-only changes.

tests/cli/expected/backlinks_en_json.txt

Whitespace-only changes.

tests/cli/expected/categories_de.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)