{"id":13615019,"url":"https://github.com/j178/leetgo","last_synced_at":"2025-05-14T17:05:46.439Z","repository":{"id":65163368,"uuid":"551311479","full_name":"j178/leetgo","owner":"j178","description":"Best LeetCode friend for geek. :snowboarder:","archived":false,"fork":false,"pushed_at":"2025-05-13T19:59:42.000Z","size":3939,"stargazers_count":577,"open_issues_count":27,"forks_count":34,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-14T17:05:03.101Z","etag":null,"topics":["chatgpt","cli","go","golang","leetcode","leetcode-cli","leetcode-contest","leetcode-helper"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/j178.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"j178"}},"created_at":"2022-10-14T07:05:17.000Z","updated_at":"2025-05-14T16:59:46.000Z","dependencies_parsed_at":"2024-02-04T04:27:47.485Z","dependency_job_id":"f88a03db-f7c9-445b-b626-4ec474b2c4d6","html_url":"https://github.com/j178/leetgo","commit_stats":{"total_commits":686,"total_committers":16,"mean_commits":42.875,"dds":"0.11807580174927113","last_synced_commit":"f9a820bbeee19fd57728e4cee60ec9ae59995960"},"previous_names":[],"tags_count":74,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j178%2Fleetgo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j178%2Fleetgo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j178%2Fleetgo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/j178%2Fleetgo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/j178","download_url":"https://codeload.github.com/j178/leetgo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254190396,"owners_count":22029632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatgpt","cli","go","golang","leetcode","leetcode-cli","leetcode-contest","leetcode-helper"],"created_at":"2024-08-01T20:01:08.255Z","updated_at":"2025-05-14T17:05:46.419Z","avatar_url":"https://github.com/j178.png","language":"Go","funding_links":["https://github.com/sponsors/j178"],"categories":["Go"],"sub_categories":[],"readme":"**[简体中文](./README_zh.md) | English**\n\n# Leetgo\n \n[![Go Report Card](https://goreportcard.com/badge/github.com/j178/leetgo)](https://goreportcard.com/report/github.com/j178/leetgo)\n[![CI](https://github.com/j178/leetgo/actions/workflows/ci.yaml/badge.svg)](https://github.com/j178/leetgo/actions/workflows/ci.yaml)\n[![GitHub downloads](https://img.shields.io/github/downloads/j178/leetgo/total)](https://github.com/j178/leetgo/releases)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://makeapullrequest.com)\n[![Twitter Follow](https://img.shields.io/twitter/follow/niceoe)](https://twitter.com/niceoe)\n\u003cimg align=\"right\" height=150 src=\"https://github.com/user-attachments/assets/5942dc93-31b5-4d93-ba55-4c0b20550981\" /\u003e\n\n`leetgo` is a command-line tool for LeetCode that provides almost all the functionality of LeetCode,\nallowing you to do all of your LeetCode exercises without leaving the terminal.\nIt can automatically generate **skeleton code and test cases**, support **local testing and debugging**,\nand you can use any IDE you like to solve problems.\n\nAnd `leetgo` also supports real-time generation of **contest questions**, submitting all questions at once, so your submissions are always one step ahead!\n\n## Quick Start\n\n1. [Install leetgo](#installation)\n2. Initialize leetgo: `leetgo init -t \u003cus or cn\u003e -l \u003clang\u003e`\n3. Edit leetgo config file: `leetgo.yaml`\n4. Pick a question: `leetgo pick \u003cid or name or today\u003e`\n5. Test your code: `leetgo test last -L`\n6. Submit your code: `leetgo submit last`\n\nYou can test and submit in one command: `leetgo test last -L -s`\n\nYou can edit the question file in your favorite editor: `leetgo edit last`\n\n## Demo\n\n![demo](https://github.com/j178/leetgo/assets/10510431/8eaee981-a1f7-4b40-b9df-5af3c72daf26)\n\n## Features\n\n- Generate description, skeleton code and testing code for a question\n- Customize the code template for generated code, use modifiers to pre-process code\n- Execute test cases on your local machine\n- Wait and generate contest questions just in time, test and submit all at once\n- Support for both leetcode.com and leetcode.cn\n- Automatically read cookies from browser, no need to enter password\n- Automatically open question files in your favourite editor\n- Use OpenAI to automatically discover and fix issues in the code (Experimental)\n\n## Language support\n\n`leetgo` supports code generation for most languages, and local testing for some languages.\n\nIn the Go language, running `leetgo pick 257` will generate the following code:\n\n```go\n// Omitted some code...\n// @lc code=begin\n\nfunc binaryTreePaths(root *TreeNode) (ans []string) {\n\n\treturn\n}\n\n// @lc code=end\n\nfunc main() {\n\tstdin := bufio.NewReader(os.Stdin)\n\troot := Deserialize[*TreeNode](ReadLine(stdin))\n\tans := binaryTreePaths(root)\n\tfmt.Println(\"output: \" + Serialize(ans))\n}\n```\n\nThis is a complete and runnable program. You can run it directly, input the test cases, and compare the results. \n`leetgo test -L` will automatically run this program with the test cases in `testcases.txt` and compare the results.\n\nLocal testing means that you can run the test cases on your local machine, so you can use a debugger to debug your code.\n\nLocal testing requires more work to implement for each language, so not all languages are supported. Below is the current support matrix:\n\n\u003c!-- BEGIN MATRIX --\u003e\n|  | Generation | Local testing |\n| --- | --- | --- |\n| Go | :white_check_mark: | :white_check_mark: |\n| Python | :white_check_mark: | :white_check_mark: |\n| C++ | :white_check_mark: | :white_check_mark: |\n| Rust | :white_check_mark: | :white_check_mark: |\n| Java | :white_check_mark: | Not yet |\n| JavaScript | :white_check_mark: | Not yet |\n| TypeScript | :white_check_mark: | Not yet |\n| PHP | :white_check_mark: | Not yet |\n| C | :white_check_mark: | Not yet |\n| C# | :white_check_mark: | Not yet |\n| Ruby | :white_check_mark: | Not yet |\n| Swift | :white_check_mark: | Not yet |\n| Kotlin | :white_check_mark: | Not yet |\n| Bash | :white_check_mark: | Not yet |\n| MySQL | :white_check_mark: | Not yet |\n| MSSQL | :white_check_mark: | Not yet |\n| Oracle | :white_check_mark: | Not yet |\n| Erlang | :white_check_mark: | Not yet |\n| Racket | :white_check_mark: | Not yet |\n| Scala | :white_check_mark: | Not yet |\n| Elixir | :white_check_mark: | Not yet |\n| Dart | :white_check_mark: | Not yet |\n\u003c!-- END MATRIX --\u003e\n\nWelcome to help us implement local testing for more languages!\n\n## Installation\n\nYou can download the latest binary from the [release page](https://github.com/j178/leetgo/releases).\n\n### Install via [HomeBrew](https://brew.sh/) on macOS/Linux\n\n```shell\nbrew install j178/tap/leetgo\n```\n\n### Install via [Scoop](https://scoop.sh/) on Windows\n\n```shell\nscoop bucket add j178 https://github.com/j178/scoop-bucket.git\nscoop install j178/leetgo\n```\n\n### Install on ArchLinux\n\n```shell\nyay -S leetgo-bin\n```\n\n### Install via installer script on macOS/Linux\n\n```shell\ncurl -fsSL https://raw.githubusercontent.com/j178/leetgo/master/scripts/install.sh | bash\n```\n\n### Install from source via `go install`\n\n```shell\ngo install github.com/j178/leetgo@latest\n```\n\n## Usage\n\u003c!-- BEGIN USAGE --\u003e\n```\nUsage:\n  leetgo [command]\n\nAvailable Commands:\n  init                    Init a leetcode workspace\n  pick                    Generate a new question\n  info                    Show question info\n  test                    Run question test cases\n  submit                  Submit solution\n  fix                     Use ChatGPT API to fix your solution code (just for fun)\n  edit                    Open solution in editor\n  contest                 Generate contest questions\n  cache                   Manage local questions cache\n  debug                   Show debug info\n  open                    Open one or multiple question pages in a browser\n  help                    Help about any command\n\nFlags:\n  -v, --version       version for leetgo\n  -l, --lang string   language of code to generate: cpp, go, python ...\n      --site string   leetcode site: cn, us\n  -y, --yes           answer yes to all prompts\n  -h, --help          help for leetgo\n\nUse \"leetgo [command] --help\" for more information about a command.\n```\n\u003c!-- END USAGE --\u003e\n\n### Question Identifier\n\nMany `leetgo` commands rely on `qid` to find the leetcode question. `qid` is a simplified question\nidentifier defined by leetgo, which includes the following forms (using the two-sum problem as an example):\n\n```shell\nleetgo pick two-sum          # `two-sum` is the question slug\nleetgo pick 1                # `1` is the question id\nleetgo pick today            # `today` means daily question\nleetgo pick yesterday        # `yesterday` means the question of yesterday\nleetgo pick today-1          # `today-1` means the question of yesterday, same as `yesterday`. `today-2`, `today-3` etc are also supported.\nleetgo contest weekly100     # `weekly100` means the 100th weekly contest\nleetgo test last             # `last` means the last generated question\nleetgo test weekly100/1      # `weekly100/1` means the first question of the 100th weekly contest\nleetgo submit b100/2         # `b100/2` means the second question of the 100th biweekly contest\nleetgo submit w99/           # `w99/` means all questions of the 99th biweekly contest (must keep the trailing slash)\nleetgo test last/1           # `last/1` means the first question of the last generated contest\nleetgo test last/            # `last/` means all questions of the last generated contest (must keep the trailing slash)\n```\n\n## Configuration\n\n\u003e [!WARNING]\n\u003e Since `v1.4`, `leetgo` no longer reads the global `~/.config/leetgo/config.yaml` file, please put all configurations in the project's `leetgo.yaml` file.\n\n`leetgo init` generates a `leetgo.yaml` file in the current directory, which contains all the configurations of `leetgo`. You can modify this file according to your needs.\n\nThe directory where `leetgo.yaml` is located is considered as the root directory of a `leetgo` project, and `leetgo` will generate all code files undeer this directory. `leetgo` will look for the `leetgo.yaml` file in the current directory. If it is not found, it will recursively search upwards until a `leetgo.yaml` file is found or the root directory of the file system is reached.\n\nBelow is the demonstration of a complete configuration:\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to expand\u003c/summary\u003e\n\n\u003c!-- BEGIN CONFIG --\u003e\n```yaml\n# Your name\nauthor: Bob\n# Language of the question description: 'zh' (Simplified Chinese) or 'en' (English).\nlanguage: zh\ncode:\n  # Language of code generated for questions: go, cpp, python, java... \n  # (will be overridden by command line flag -l/--lang).\n  lang: go\n  # The default template to generate filename (without extension), e.g. {{.Id}}.{{.Slug}}\n  # Available attributes: Id, Slug, Title, Difficulty, Lang, SlugIsMeaningful\n  # (Most questions have descriptive slugs, but some consist of random characters. The SlugIsMeaningful boolean indicates whether a slug is meaningful.)\n  # Available functions: lower, upper, trim, padWithZero, toUnderscore, group.\n  filename_template: '{{ .Id | padWithZero 4 }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'\n  # Generate question description into a separate question.md file, otherwise it will be embed in the code file.\n  separate_description_file: true\n  # Default modifiers for all languages.\n  modifiers:\n    - name: removeUselessComments\n  go:\n    # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp.\n    out_dir: go\n    # Functions that modify the generated code.\n    modifiers:\n      - name: removeUselessComments\n      - name: changeReceiverName\n      - name: addNamedReturn\n      - name: addMod\n  python3:\n    # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp.\n    out_dir: python\n    # Path to the python executable that creates the venv.\n    executable: python3\n  cpp:\n    # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp.\n    out_dir: cpp\n    # C++ compiler\n    cxx: g++\n    # C++ compiler flags (our Leetcode I/O library implementation requires C++17).\n    cxxflags: -O2 -std=c++17\n  rust:\n    # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp.\n    out_dir: rust\n  java:\n    # Base directory to put generated questions, defaults to the language slug, e.g. go, python, cpp.\n    out_dir: java\nleetcode:\n  # LeetCode site, https://leetcode.com or https://leetcode.cn\n  site: https://leetcode.cn\n  # Credentials to access LeetCode.\n  credentials:\n    # How to provide credentials: browser, cookies, password or none.\n    from:\n      - browser\n    # Browsers to get cookies from: chrome, safari, edge or firefox. If empty, all browsers will be tried. Only used when 'from' is 'browser'.\n    browsers: []\ncontest:\n  # Base directory to put generated contest questions.\n  out_dir: contest\n  # Template to generate filename of the question.\n  filename_template: '{{ .ContestShortSlug }}/{{ .Id }}{{ if .SlugIsMeaningful }}.{{ .Slug }}{{ end }}'\n  # Open the contest page in browser after generating.\n  open_in_browser: true\n# Editor settings to open generated files.\neditor:\n  # Use a predefined editor: vim, vscode, goland\n  # Set to 'none' to disable, set to 'custom' to provide your own command and args.\n  use: none\n  # Custom command to open files.\n  command: \"\"\n  # Arguments to your custom command.\n  # String contains {{.CodeFile}}, {{.TestFile}}, {{.DescriptionFile}}, {{.TestCasesFile}} will be replaced with corresponding file path.\n  # {{.Folder}} will be substituted with the output directory.\n  # {{.Files}} will be substituted with the list of all file paths.\n  args: \"\"\n```\n\u003c!-- END CONFIG --\u003e\n\u003c/details\u003e\n\n## LeetCode Credentials\n\n`leetgo` uses LeetCode's GraphQL API to retrieve questions and submit solutions. `leetgo` needs your LeetCode cookies to access the authenticated API.\n\nThere are three ways to make cookies available to `leetgo`:\n\n- Read cookies from browser automatically.\n\n  Currently, `leetgo` supports Chrome, FireFox, Safari[^1], Edge.\n\n  ```yaml\n  leetcode:\n    credentials:\n      from: browser\n  ```\n\n  \u003e [!IMPORTANT]  \n  On Windows, Chrome/Edge v127 enabled [App-Bound Encryption](https://security.googleblog.com/2024/07/improving-security-of-chrome-cookies-on.html) and `leetgo` can no longer decrypt cookies from Chrome/Edge.\n  You would need to provide cookies manually or use other browsers.\n\n- Provide cookies.\n\n  You can get your cookies named `LEETCODE_SESSION` and `csrftoken` from browser's developer tools, and set the `LEETCODE_SESSION` and `LEETCODE_CSRFTOKEN` environment variables.  If you are using `leetcode.com`, `LEETCODE_CFCLEARANCE` should also be set to the value of the `cf_clearance` cookie.\n\n  ```yaml\n  leetcode:\n    credentials:\n      from: cookies\n  ```\n\n- Provide username and password through `LEETCODE_USERNAME` and `LEETCODE_PASSWORD` environment variables.\n\n  ```yaml\n  leetcode:\n    credentials:\n      from: password\n  ```\n\n\u003e [!TIP]\n\u003e You can specify which browser to read cookies from, e.g. `browsers: [chrome]`.  \n\u003e You can specify multiple authentication methods, `leetgo` will try them in order, e.g. `from: [browser, cookies]`.  \n\u003e You can put all the environment variables in a `.env` file in the project's root directory, `leetgo` will automatically read them.  \n\n\u003e [!NOTE]\n\u003e Password authentication is not recommended, and it is not supported by `leetcode.com`.\n\nYou can put environment variables in a `.env` file in the project's root directory, and `leetgo` will automatically read them.\n\n## Advanced Usage\n\n### `testcases.txt`\n\n`testcasts.txt` is generated by `leetgo` and contains all the test cases of the question.\n\nYou can add a new test case by specifying only the input and leaving the output blank. When you run `leetgo test` (without `-L`), the expected output will be retrieved from the remote server.\nFor example:\n\n```\ninput:\n[3,3]\n6\noutput:\n\ninput:\n[1,2,3,4]\n7\noutput:\n```\n\n### Templates\n\nSeveral fields in leetgo's config file support templating. These fields are often suffixed with `_template`.\nYou can use custom template to generate your own filename, code, etc.\n\n### Blocks\n\nA code file is composed of different blocks, you can overwrite some of them to provide your own snippets.\n\n| Available blocks |\n| -- |\n| header |\n| description |\n| title |\n| beforeMarker |\n| beforeCode |\n| code |\n| afterCode |\n| afterMarker |\n\nFor example:\n```yaml\ncode:\nlang: cpp\ncpp:\n  blocks:\n  - name: beforeCode\n    template: |\n      #include \u003ciostream\u003e\n      using namespace std;\n  - name: afterMarker\n    template: |\n      int main() {}\n```\n\n### Scripting\n\n`leetgo` supports providing a JavaScript function to handle the code before generation, for example:\n\n```yaml\ncode:\n  lang: cpp\n  cpp:\n    modifiers:\n    - name: removeUselessComments\n    - script: |\n        function modify(code) {\n          return \"// hello world\\n\" + code;\n        }\n```\n\n## FAQ\n\nIf you encounter any problems, please run your command with the `DEBUG` environment variable set to `1`, copy the command output, and open an issue.\n\nSome common problems can be found in the [Q\u0026A](https://github.com/j178/leetgo/discussions/categories/q-a) page.\n\n## Contributions welcome!\n\n[Good First Issues](https://github.com/j178/leetgo/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) are a good place to start,\nand you can also check out some [Help Wanted](https://github.com/j178/leetgo/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) issues.\n\nIf you want to add local testing support for a new language, please refer to [#112](https://github.com/j178/leetgo/issues/112).\n\nBefore submitting a PR, please run `golangci-lint run --fix` to fix lint errors.\n\n## Credits\n\nHere are some awesome projects that inspired me to create this project:\n\n- https://github.com/EndlessCheng/codeforces-go\n- https://github.com/clearloop/leetcode-cli\n- https://github.com/budougumi0617/leetgode\n- https://github.com/skygragon/leetcode-cli\n\nAlso thanks to [JetBrains](https://www.jetbrains.com/) for providing free licenses to support this project.\n\n[![JetBrains Logo](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://jb.gg/OpenSourceSupport)\n\n[^1]: For Safari on macOS, you may need to grant `Full Disk Access` privilege to your terminal app which you would like to run `leetgo`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj178%2Fleetgo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fj178%2Fleetgo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fj178%2Fleetgo/lists"}