"""api_fetcher.py - Fetch data from APIs with configurable options."""
import argparse
def fetch_data(api_name: str, resource: str, limit: int, output_file: str = None):
"""Fetch data from an API (implementation details omitted)."""
print(f"Fetching {limit} {resource} from {api_name}...")
if output_file:
print(f"Saving to {output_file}")
# Actual API call would go here
return {"status": "success", "count": limit}
def main():
parser = argparse.ArgumentParser(
description="Fetch data from various APIs"
)
# Positional arguments
parser.add_argument("api", help="API name (e.g., 'pokemon', 'jokes', 'weather')")
parser.add_argument("resource", help="Resource to fetch (e.g., 'pikachu', 'random')")
# Optional arguments with types
parser.add_argument(
"-l", "--limit",
type=int,
default=10,
help="Number of items to fetch (default: 10)"
)
parser.add_argument(
"-o", "--output",
help="Output file path (if not specified, prints to console)"
)
parser.add_argument(
"--format",
choices=["json", "csv", "txt"],
default="json",
help="Output format (default: json)"
)
args = parser.parse_args()
# Call the function with parsed arguments
result = fetch_data(args.api, args.resource, args.limit, args.output)
print(f"Result: {result}")
if __name__ == "__main__":
main()