mirror of
				https://github.com/Tyrrrz/CliFx.git
				synced 2025-10-25 15:19:17 +00:00 
			
		
		
		
	Compare commits
	
		
			350 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | ca8368e1d9 | ||
|  | d4b6a2b7a9 | ||
|  | 63f57025e8 | ||
|  | 25083eff44 | ||
|  | 7f2c00fe3a | ||
|  | 7638b997ff | ||
|  | d80d012938 | ||
|  | 2a02d39dba | ||
|  | c40b4f3501 | ||
|  | 3fb2a2319b | ||
|  | 1a5a0374c7 | ||
|  | 432c8a66af | ||
|  | 078ddeaf07 | ||
|  | 0fa2ebc636 | ||
|  | c79a8c6502 | ||
|  | cfbd8f9e76 | ||
|  | e329f0fc78 | ||
|  | 357426c536 | ||
|  | bc2164499b | ||
|  | 20481d4e24 | ||
|  | 2cb9335e25 | ||
|  | f5ff6193e8 | ||
|  | 36b2b07a1d | ||
|  | 73bf19d766 | ||
|  | 093b6767c4 | ||
|  | e4671e50bb | ||
|  | 40beb283d5 | ||
|  | 71fe231f28 | ||
|  | 8546c54c23 | ||
|  | 0fc88a42ba | ||
|  | cb8f4b122e | ||
|  | 540f307f42 | ||
|  | a62ce71424 | ||
|  | ab48098e06 | ||
|  | 0532d724a1 | ||
|  | 545c7c3fbd | ||
|  | a813436577 | ||
|  | fcc93603a7 | ||
|  | 2d3c221b48 | ||
|  | 651146c97b | ||
|  | 82b0c6fd98 | ||
|  | a4376c955b | ||
|  | f7645afbdb | ||
|  | e20672328b | ||
|  | e99a95ef7c | ||
|  | 3e7eb08eca | ||
|  | cfd28c133e | ||
|  | 034d3cec66 | ||
|  | 3fc7054f80 | ||
|  | 2323a57c39 | ||
|  | bcb34055ac | ||
|  | 24fd87b1e1 | ||
|  | fe935b5775 | ||
|  | 7dcd523bfe | ||
|  | cad1c14474 | ||
|  | 57db910489 | ||
|  | ae9c4e6d1e | ||
|  | 30bc1d3330 | ||
|  | a5a4ad05a0 | ||
|  | 0b77895ca5 | ||
|  | 54994755b1 | ||
|  | aee63cb9f2 | ||
|  | 4bdd3ccc6c | ||
|  | 6aa72e45e8 | ||
|  | 76e8d47e03 | ||
|  | 6304b8ab9c | ||
|  | 98b50d0e8e | ||
|  | 5aea869c2a | ||
|  | 425c8f4022 | ||
|  | 490398f773 | ||
|  | 5854f36756 | ||
|  | ec6c72e6a3 | ||
|  | 41bc64be4a | ||
|  | 7df0e77e4d | ||
|  | 914e8e17cd | ||
|  | 40f106d0b0 | ||
|  | 566dd4a9a7 | ||
|  | 9beb439323 | ||
|  | 029257a915 | ||
|  | d330fbbb63 | ||
|  | 236867f547 | ||
|  | b41e9b4929 | ||
|  | ff06b8896f | ||
|  | 0fe9c89fa0 | ||
|  | 8646c9de5e | ||
|  | a33c42a163 | ||
|  | 55cea48cbd | ||
|  | e67eda3515 | ||
|  | 4412c20e97 | ||
|  | 9eb84c6649 | ||
|  | 2ef37ab6d9 | ||
|  | 38a73772fc | ||
|  | aed53eb090 | ||
|  | 21b601da66 | ||
|  | a4726fcefd | ||
|  | ab24ca8604 | ||
|  | 3533bff344 | ||
|  | 1b096b679e | ||
|  | cb61b31e9d | ||
|  | d8f183c404 | ||
|  | c95b6c32d5 | ||
|  | d2e390c691 | ||
|  | 66ef221586 | ||
|  | 2d3bb30125 | ||
|  | 5d72692aa5 | ||
|  | 3be17db784 | ||
|  | 4aef8ce8fb | ||
|  | 8c1cff3bb7 | ||
|  | 669d8bfe20 | ||
|  | 4dce7bddb4 | ||
|  | a621e89e89 | ||
|  | 5ea11e3a23 | ||
|  | 7cb61182d2 | ||
|  | 99c59431c4 | ||
|  | f376081489 | ||
|  | 00a1e12b5c | ||
|  | 81f8b17451 | ||
|  | aa8315b68d | ||
|  | e52781c25a | ||
|  | 01f29a5375 | ||
|  | 013cb8f66b | ||
|  | 9c715f458e | ||
|  | 90d93a57ee | ||
|  | 8da4a61eb7 | ||
|  | f718370642 | ||
|  | 83c7af72bf | ||
|  | eff84fd7ae | ||
|  | f66fa97b87 | ||
|  | 9f309b5d4a | ||
|  | 456099591a | ||
|  | bf7f607f9b | ||
|  | a4041ab019 | ||
|  | a0fde872ec | ||
|  | f0c040c7b9 | ||
|  | a09818d452 | ||
|  | 1c331df4b1 | ||
|  | dc20fe9730 | ||
|  | 31ae0271b9 | ||
|  | 6ed6d2ced9 | ||
|  | 01a4846159 | ||
|  | 02dc7de127 | ||
|  | a1ff1a1539 | ||
|  | a02951f755 | ||
|  | 7cb25254e8 | ||
|  | 3d9ad16117 | ||
|  | d0ad3bc45d | ||
|  | 6541ce568d | ||
|  | 32d3b66185 | ||
|  | 48f157a51e | ||
|  | b1995fa4f7 | ||
|  | 74bc973f77 | ||
|  | 3420c3d039 | ||
|  | b10577fec5 | ||
|  | af96d0d31d | ||
|  | bd29ad31cc | ||
|  | 15150cb3ed | ||
|  | aac9c968eb | ||
|  | 85a9f157ad | ||
|  | d24a79361d | ||
|  | 5785720f31 | ||
|  | 3f6f0b9f1b | ||
|  | 128bb5be8b | ||
|  | 36b3814f4e | ||
|  | c4a975d5f1 | ||
|  | 79d86d39c1 | ||
|  | c476700168 | ||
|  | 5e97ebe7f0 | ||
|  | 64cbdaaeab | ||
|  | ae1f03914c | ||
|  | ff25dccf8a | ||
|  | 6e0d881682 | ||
|  | 89fd42888a | ||
|  | eeac82a6e7 | ||
|  | c641c6fbe2 | ||
|  | 5ec732fe9a | ||
|  | 6d87411dbf | ||
|  | ed3054c855 | ||
|  | 5d00de4dfe | ||
|  | 016ec8b186 | ||
|  | 9141092919 | ||
|  | 1fe97b0140 | ||
|  | 6ad5989c25 | ||
|  | 7e1db916fc | ||
|  | 1c69d5c80d | ||
|  | ab87225f1f | ||
|  | 6d33c5cdad | ||
|  | e4c899c6c2 | ||
|  | 35b3ad0d63 | ||
|  | 4e70557b47 | ||
|  | 0a8d58255a | ||
|  | d3fbc9c643 | ||
|  | 1cbf8776be | ||
|  | 16e33f7b8f | ||
|  | 5c848056c5 | ||
|  | 864efd3179 | ||
|  | 7f206a0c77 | ||
|  | 22c15f8ec6 | ||
|  | 59373eadc2 | ||
|  | ed3e4f471e | ||
|  | 41cb8647b5 | ||
|  | c7015181e1 | ||
|  | 86742755e8 | ||
|  | 33f95d941d | ||
|  | 1328592cb5 | ||
|  | 0711b863ea | ||
|  | a2f5cd54be | ||
|  | 7836ec610f | ||
|  | 2e489927eb | ||
|  | 02e8d19e48 | ||
|  | eb7107fb0a | ||
|  | a396009b62 | ||
|  | 1d9c7e942c | ||
|  | 0f3abb9db4 | ||
|  | 896482821c | ||
|  | aa3094ee54 | ||
|  | 712580e3d7 | ||
|  | c08102f85f | ||
|  | 5e684c8b36 | ||
|  | 300ae70564 | ||
|  | 76f0c77f1e | ||
|  | 0f7cea4ed1 | ||
|  | 32ee0b2bd6 | ||
|  | 4ff1e1d3e1 | ||
|  | 8e96d2701d | ||
|  | 8e307df231 | ||
|  | ff38f4916a | ||
|  | 7cbbb220b4 | ||
|  | ae2d4299f0 | ||
|  | 21bc69d116 | ||
|  | 05a70175cc | ||
|  | 33ec2eb3a0 | ||
|  | f6ef6cd4c0 | ||
|  | a9ef693dc1 | ||
|  | 98bbd666dc | ||
|  | 4e7ed830f8 | ||
|  | ef87ff76fc | ||
|  | 2feeb21270 | ||
|  | 9990387cfa | ||
|  | bc1bdca7c6 | ||
|  | 2a992d37df | ||
|  | 15c87aecbb | ||
|  | 10a46451ac | ||
|  | e4c6a4174b | ||
|  | 4c65f7bbee | ||
|  | 5f21de0df5 | ||
|  | 9b01b67d98 | ||
|  | 4508f5e211 | ||
|  | f0cbc46df4 | ||
|  | 6c96e9e173 | ||
|  | 51cca36d2a | ||
|  | 84672c92f6 | ||
|  | b1d01898b6 | ||
|  | 441a47a1a8 | ||
|  | 8abd7219a1 | ||
|  | df73a0bfe8 | ||
|  | 55d12dc721 | ||
|  | a6ee44c1bb | ||
|  | 76816e22f1 | ||
|  | daf25e59d6 | ||
|  | f2b4e53615 | ||
|  | 2d519ab190 | ||
|  | 2d479c9cb6 | ||
|  | 2bb7e13e51 | ||
|  | 6e1dfdcdd4 | ||
|  | 5ba647e5c1 | ||
|  | 853492695f | ||
|  | d5d72c7c50 | ||
|  | d676b5832e | ||
|  | 28097afc1e | ||
|  | fda96586f3 | ||
|  | fc5af8dbbc | ||
|  | 4835e64388 | ||
|  | 0999c33f93 | ||
|  | 595805255a | ||
|  | 65eaa912cf | ||
|  | 038f48b78e | ||
|  | d7460244b7 | ||
|  | 02766868fc | ||
|  | 8d7d25a144 | ||
|  | 17ded54e24 | ||
|  | 54a4c32ddf | ||
|  | 6d46e82145 | ||
|  | fd4a2a18fe | ||
|  | bfe99d620e | ||
|  | c5a111207f | ||
|  | 544945c0e6 | ||
|  | c616cdd750 | ||
|  | d3c396956d | ||
|  | d0cbbc6d9a | ||
|  | 49c7905150 | ||
|  | f5a992a16e | ||
|  | bade0a0048 | ||
|  | 7d3d79b861 | ||
|  | 58df63a7ad | ||
|  | b938eef013 | ||
|  | 94f63631db | ||
|  | 90d1b11430 | ||
|  | 550e54b86d | ||
|  | 90a01e729b | ||
|  | ac01c2aecb | ||
|  | 4acffe925c | ||
|  | 18f53eeeef | ||
|  | 03d6942540 | ||
|  | 9be811a89a | ||
|  | f9f5a4696b | ||
|  | d6da687170 | ||
|  | eba66d0878 | ||
|  | 8c682766bd | ||
|  | 39d626c8d8 | ||
|  | a338ac8ce2 | ||
|  | 11637127cb | ||
|  | 4e12aefafb | ||
|  | 144d3592fb | ||
|  | 6f82c2f0f9 | ||
|  | b8c60717d5 | ||
|  | fec6850c39 | ||
|  | 6a378ad946 | ||
|  | 11579f11b1 | ||
|  | 60a3b26fd1 | ||
|  | 3abdfb1acf | ||
|  | 9557d386e2 | ||
|  | d0d024c427 | ||
|  | f765af6061 | ||
|  | 7f2202e869 | ||
|  | 14ad9d5738 | ||
|  | b120138de3 | ||
|  | 8df1d607c1 | ||
|  | c06f2810b9 | ||
|  | d52a205f13 | ||
|  | 0ec12e57c1 | ||
|  | c322b7029c | ||
|  | 6a38c04c11 | ||
|  | 5e53107def | ||
|  | 36cea937de | ||
|  | 438d6b98ac | ||
|  | 8e1488c395 | ||
|  | 65d321b476 | ||
|  | c6d2359d6b | ||
|  | 0d32876bad | ||
|  | c063251d89 | ||
|  | 3831cfc7c0 | ||
|  | b17341b56c | ||
|  | 5bda964fb5 | ||
|  | 432430489a | ||
|  | 9a20101f30 | ||
|  | b491818779 | ||
|  | 69c24c8dfc | ||
|  | 004f906148 | ||
|  | ac83233dc2 | ||
|  | 082910c968 | 
							
								
								
									
										
											BIN
										
									
								
								.assets/help-screen.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								.assets/help-screen.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 14 KiB | 
							
								
								
									
										63
									
								
								.gitattributes
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										63
									
								
								.gitattributes
									
									
									
									
										vendored
									
									
								
							| @@ -1,63 +0,0 @@ | |||||||
| ############################################################################### |  | ||||||
| # Set default behavior to automatically normalize line endings. |  | ||||||
| ############################################################################### |  | ||||||
| * text=auto |  | ||||||
|  |  | ||||||
| ############################################################################### |  | ||||||
| # Set default behavior for command prompt diff. |  | ||||||
| # |  | ||||||
| # This is need for earlier builds of msysgit that does not have it on by |  | ||||||
| # default for csharp files. |  | ||||||
| # Note: This is only used by command line |  | ||||||
| ############################################################################### |  | ||||||
| #*.cs     diff=csharp |  | ||||||
|  |  | ||||||
| ############################################################################### |  | ||||||
| # Set the merge driver for project and solution files |  | ||||||
| # |  | ||||||
| # Merging from the command prompt will add diff markers to the files if there |  | ||||||
| # are conflicts (Merging from VS is not affected by the settings below, in VS |  | ||||||
| # the diff markers are never inserted). Diff markers may cause the following  |  | ||||||
| # file extensions to fail to load in VS. An alternative would be to treat |  | ||||||
| # these files as binary and thus will always conflict and require user |  | ||||||
| # intervention with every merge. To do so, just uncomment the entries below |  | ||||||
| ############################################################################### |  | ||||||
| #*.sln       merge=binary |  | ||||||
| #*.csproj    merge=binary |  | ||||||
| #*.vbproj    merge=binary |  | ||||||
| #*.vcxproj   merge=binary |  | ||||||
| #*.vcproj    merge=binary |  | ||||||
| #*.dbproj    merge=binary |  | ||||||
| #*.fsproj    merge=binary |  | ||||||
| #*.lsproj    merge=binary |  | ||||||
| #*.wixproj   merge=binary |  | ||||||
| #*.modelproj merge=binary |  | ||||||
| #*.sqlproj   merge=binary |  | ||||||
| #*.wwaproj   merge=binary |  | ||||||
|  |  | ||||||
| ############################################################################### |  | ||||||
| # behavior for image files |  | ||||||
| # |  | ||||||
| # image files are treated as binary by default. |  | ||||||
| ############################################################################### |  | ||||||
| #*.jpg   binary |  | ||||||
| #*.png   binary |  | ||||||
| #*.gif   binary |  | ||||||
|  |  | ||||||
| ############################################################################### |  | ||||||
| # diff behavior for common document formats |  | ||||||
| #  |  | ||||||
| # Convert binary document formats to text before diffing them. This feature |  | ||||||
| # is only available from the command line. Turn it on by uncommenting the  |  | ||||||
| # entries below. |  | ||||||
| ############################################################################### |  | ||||||
| #*.doc   diff=astextplain |  | ||||||
| #*.DOC   diff=astextplain |  | ||||||
| #*.docx  diff=astextplain |  | ||||||
| #*.DOCX  diff=astextplain |  | ||||||
| #*.dot   diff=astextplain |  | ||||||
| #*.DOT   diff=astextplain |  | ||||||
| #*.pdf   diff=astextplain |  | ||||||
| #*.PDF   diff=astextplain |  | ||||||
| #*.rtf   diff=astextplain |  | ||||||
| #*.RTF   diff=astextplain |  | ||||||
							
								
								
									
										3
									
								
								.github/FUNDING.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.github/FUNDING.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,3 +0,0 @@ | |||||||
| github: Tyrrrz |  | ||||||
| patreon: Tyrrrz |  | ||||||
| custom: ['buymeacoffee.com/Tyrrrz', 'tyrrrz.me/donate'] |  | ||||||
							
								
								
									
										77
									
								
								.github/ISSUE_TEMPLATE/bug-report.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								.github/ISSUE_TEMPLATE/bug-report.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,77 @@ | |||||||
|  | name: 🐛 Bug report | ||||||
|  | description: Report broken functionality. | ||||||
|  | labels: [bug] | ||||||
|  |  | ||||||
|  | body: | ||||||
|  |   - type: markdown | ||||||
|  |     attributes: | ||||||
|  |       value: | | ||||||
|  |         - Avoid generic or vague titles such as "Something's not working" or "A couple of problems" — be as descriptive as possible. | ||||||
|  |         - Keep your issue focused on one single problem. If you have multiple bug reports, please create a separate issue for each of them. | ||||||
|  |         - Issues should represent **complete and actionable** work items. If you are unsure about something or have a question, please start a [discussion](https://github.com/Tyrrrz/CliFx/discussions/new) instead. | ||||||
|  |         - Remember that **CliFx** is an open-source project funded by the community. If you find it useful, **please consider [donating](https://tyrrrz.me/donate) to support its development**. | ||||||
|  |  | ||||||
|  |         ___ | ||||||
|  |  | ||||||
|  |   - type: input | ||||||
|  |     attributes: | ||||||
|  |       label: Version | ||||||
|  |       description: Which version of the package does this bug affect? Make sure you're not using an outdated version. | ||||||
|  |       placeholder: v1.0.0 | ||||||
|  |     validations: | ||||||
|  |       required: true | ||||||
|  |  | ||||||
|  |   - type: input | ||||||
|  |     attributes: | ||||||
|  |       label: Platform | ||||||
|  |       description: Which platform do you experience this bug on? | ||||||
|  |       placeholder: .NET 7.0 / Windows 11 | ||||||
|  |     validations: | ||||||
|  |       required: true | ||||||
|  |  | ||||||
|  |   - type: textarea | ||||||
|  |     attributes: | ||||||
|  |       label: Steps to reproduce | ||||||
|  |       description: > | ||||||
|  |         Minimum steps required to reproduce the bug, including prerequisites, code snippets, or other relevant items. | ||||||
|  |         The information provided in this field must be readily actionable, meaning that anyone should be able to reproduce the bug by following these steps. | ||||||
|  |         If the reproduction steps are too complex to fit in this field, please provide a link to a repository instead. | ||||||
|  |       placeholder: | | ||||||
|  |         - Step 1 | ||||||
|  |         - Step 2 | ||||||
|  |         - Step 3 | ||||||
|  |     validations: | ||||||
|  |       required: true | ||||||
|  |  | ||||||
|  |   - type: textarea | ||||||
|  |     attributes: | ||||||
|  |       label: Details | ||||||
|  |       description: Clear and thorough explanation of the bug, including any additional information you may find relevant. | ||||||
|  |       placeholder: | | ||||||
|  |         - Expected behavior: ... | ||||||
|  |         - Actual behavior: ... | ||||||
|  |     validations: | ||||||
|  |       required: true | ||||||
|  |  | ||||||
|  |   - type: checkboxes | ||||||
|  |     attributes: | ||||||
|  |       label: Checklist | ||||||
|  |       description: Quick list of checks to ensure that everything is in order. | ||||||
|  |       options: | ||||||
|  |         - label: I have looked through existing issues to make sure that this bug has not been reported before | ||||||
|  |           required: true | ||||||
|  |         - label: I have provided a descriptive title for this issue | ||||||
|  |           required: true | ||||||
|  |         - label: I have made sure that this bug is reproducible on the latest version of the package | ||||||
|  |           required: true | ||||||
|  |         - label: I have provided all the information needed to reproduce this bug as efficiently as possible | ||||||
|  |           required: true | ||||||
|  |         - label: I have sponsored this project | ||||||
|  |           required: false | ||||||
|  |         - label: I have not read any of the above and just checked all the boxes to submit the issue | ||||||
|  |           required: false | ||||||
|  |  | ||||||
|  |   - type: markdown | ||||||
|  |     attributes: | ||||||
|  |       value: | | ||||||
|  |         If you are struggling to provide actionable reproduction steps, or if something else is preventing you from creating a complete bug report, please start a [discussion](https://github.com/Tyrrrz/CliFx/discussions/new) instead. | ||||||
							
								
								
									
										11
									
								
								.github/ISSUE_TEMPLATE/config.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								.github/ISSUE_TEMPLATE/config.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | |||||||
|  | blank_issues_enabled: false | ||||||
|  | contact_links: | ||||||
|  |   - name: ⚠ Feature request | ||||||
|  |     url: https://github.com/Tyrrrz/.github/blob/master/docs/project-status.md | ||||||
|  |     about: Sorry, but this project is in maintenance mode and no longer accepts new feature requests. | ||||||
|  |   - name: 🗨 Discussions | ||||||
|  |     url: https://github.com/Tyrrrz/CliFx/discussions/new | ||||||
|  |     about: Ask and answer questions. | ||||||
|  |   - name: 💬 Discord server | ||||||
|  |     url: https://discord.gg/2SUWKFnHSm | ||||||
|  |     about: Chat with the project community. | ||||||
							
								
								
									
										22
									
								
								.github/dependabot.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								.github/dependabot.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | version: 2 | ||||||
|  | updates: | ||||||
|  |   - package-ecosystem: github-actions | ||||||
|  |     directory: "/" | ||||||
|  |     schedule: | ||||||
|  |       interval: monthly | ||||||
|  |     labels: | ||||||
|  |       - enhancement | ||||||
|  |     groups: | ||||||
|  |       actions: | ||||||
|  |         patterns: | ||||||
|  |           - "*" | ||||||
|  |   - package-ecosystem: nuget | ||||||
|  |     directory: "/" | ||||||
|  |     schedule: | ||||||
|  |       interval: monthly | ||||||
|  |     labels: | ||||||
|  |       - enhancement | ||||||
|  |     groups: | ||||||
|  |       nuget: | ||||||
|  |         patterns: | ||||||
|  |           - "*" | ||||||
							
								
								
									
										25
									
								
								.github/workflows/CD.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										25
									
								
								.github/workflows/CD.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,25 +0,0 @@ | |||||||
| name: CD |  | ||||||
|  |  | ||||||
| on: |  | ||||||
|   push: |  | ||||||
|     tags: |  | ||||||
|     - '*' |  | ||||||
|  |  | ||||||
| jobs: |  | ||||||
|   build: |  | ||||||
|     runs-on: ubuntu-latest |  | ||||||
|  |  | ||||||
|     steps: |  | ||||||
|     - name: Checkout |  | ||||||
|       uses: actions/checkout@v2 |  | ||||||
|  |  | ||||||
|     - name: Install .NET Core |  | ||||||
|       uses: actions/setup-dotnet@v1.4.0 |  | ||||||
|       with: |  | ||||||
|         dotnet-version: 3.1.100 |  | ||||||
|  |  | ||||||
|     - name: Pack |  | ||||||
|       run: dotnet pack CliFx --configuration Release |  | ||||||
|  |  | ||||||
|     - name: Deploy |  | ||||||
|       run: dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{ secrets.NUGET_TOKEN }} |  | ||||||
							
								
								
									
										35
									
								
								.github/workflows/CI.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										35
									
								
								.github/workflows/CI.yml
									
									
									
									
										vendored
									
									
								
							| @@ -1,35 +0,0 @@ | |||||||
| name: CI |  | ||||||
|  |  | ||||||
| on: [push, pull_request] |  | ||||||
|  |  | ||||||
| jobs: |  | ||||||
|   build: |  | ||||||
|     runs-on: ${{ matrix.os }} |  | ||||||
|  |  | ||||||
|     strategy: |  | ||||||
|       matrix: |  | ||||||
|         os: [ubuntu-latest, windows-latest, macos-latest] |  | ||||||
|  |  | ||||||
|     steps: |  | ||||||
|     - name: Checkout |  | ||||||
|       uses: actions/checkout@v2 |  | ||||||
|  |  | ||||||
|     - name: Install .NET Core |  | ||||||
|       uses: actions/setup-dotnet@v1.4.0 |  | ||||||
|       with: |  | ||||||
|         dotnet-version: 3.1.100 |  | ||||||
|  |  | ||||||
|     - name: Build & test |  | ||||||
|       run: dotnet test --configuration Release --logger GitHubActions |  | ||||||
|  |  | ||||||
|     - name: Upload coverage |  | ||||||
|       uses: codecov/codecov-action@v1.0.5 |  | ||||||
|       with: |  | ||||||
|         token: ${{ secrets.CODECOV_TOKEN }} |  | ||||||
|         file: CliFx.Tests/bin/Release/Coverage.xml |  | ||||||
|  |  | ||||||
|     - name: Upload coverage (analyzers) |  | ||||||
|       uses: codecov/codecov-action@v1.0.5 |  | ||||||
|       with: |  | ||||||
|         token: ${{ secrets.CODECOV_TOKEN }} |  | ||||||
|         file: CliFx.Analyzers.Tests/bin/Release/Coverage.xml |  | ||||||
							
								
								
									
										34
									
								
								.github/workflows/main.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								.github/workflows/main.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | |||||||
|  | name: main | ||||||
|  |  | ||||||
|  | on: | ||||||
|  |   workflow_dispatch: | ||||||
|  |     inputs: | ||||||
|  |       package-version: | ||||||
|  |         type: string | ||||||
|  |         description: Package version | ||||||
|  |         required: false | ||||||
|  |       deploy: | ||||||
|  |         type: boolean | ||||||
|  |         description: Deploy package | ||||||
|  |         required: false | ||||||
|  |         default: false | ||||||
|  |   push: | ||||||
|  |     branches: | ||||||
|  |       - master | ||||||
|  |     tags: | ||||||
|  |       - "*" | ||||||
|  |   pull_request: | ||||||
|  |     branches: | ||||||
|  |       - master | ||||||
|  |  | ||||||
|  | jobs: | ||||||
|  |   main: | ||||||
|  |     uses: Tyrrrz/.github/.github/workflows/nuget.yml@master | ||||||
|  |     with: | ||||||
|  |       deploy: ${{ inputs.deploy || github.ref_type == 'tag' }} | ||||||
|  |       package-version: ${{ inputs.package-version || (github.ref_type == 'tag' && github.ref_name) || format('0.0.0-ci-{0}', github.sha) }} | ||||||
|  |       dotnet-version: 9.0.x | ||||||
|  |     secrets: | ||||||
|  |       CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||||||
|  |       NUGET_TOKEN: ${{ secrets.NUGET_TOKEN }} | ||||||
|  |       DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }} | ||||||
							
								
								
									
										341
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										341
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,341 +1,12 @@ | |||||||
| ## Ignore Visual Studio temporary files, build results, and |  | ||||||
| ## files generated by popular Visual Studio add-ons. |  | ||||||
| ## |  | ||||||
| ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore |  | ||||||
|  |  | ||||||
| # User-specific files | # User-specific files | ||||||
| *.rsuser | .vs/ | ||||||
|  | .idea/ | ||||||
| *.suo | *.suo | ||||||
| *.user | *.user | ||||||
| *.userosscache |  | ||||||
| *.sln.docstates |  | ||||||
|  |  | ||||||
| # User-specific files (MonoDevelop/Xamarin Studio) |  | ||||||
| *.userprefs |  | ||||||
|  |  | ||||||
| # Build results | # Build results | ||||||
| [Dd]ebug/ | bin/ | ||||||
| [Dd]ebugPublic/ | obj/ | ||||||
| [Rr]elease/ |  | ||||||
| [Rr]eleases/ |  | ||||||
| x64/ |  | ||||||
| x86/ |  | ||||||
| [Aa][Rr][Mm]/ |  | ||||||
| [Aa][Rr][Mm]64/ |  | ||||||
| bld/ |  | ||||||
| [Bb]in/ |  | ||||||
| [Oo]bj/ |  | ||||||
| [Ll]og/ |  | ||||||
|  |  | ||||||
| # Visual Studio 2015/2017 cache/options directory | # Test results | ||||||
| .vs/ | TestResults/ | ||||||
| # Uncomment if you have tasks that create the project's static files in wwwroot |  | ||||||
| #wwwroot/ |  | ||||||
|  |  | ||||||
| # Visual Studio 2017 auto generated files |  | ||||||
| Generated\ Files/ |  | ||||||
|  |  | ||||||
| # MSTest test Results |  | ||||||
| [Tt]est[Rr]esult*/ |  | ||||||
| [Bb]uild[Ll]og.* |  | ||||||
|  |  | ||||||
| # NUNIT |  | ||||||
| *.VisualState.xml |  | ||||||
| TestResult.xml |  | ||||||
|  |  | ||||||
| # Build Results of an ATL Project |  | ||||||
| [Dd]ebugPS/ |  | ||||||
| [Rr]eleasePS/ |  | ||||||
| dlldata.c |  | ||||||
|  |  | ||||||
| # Benchmark Results |  | ||||||
| BenchmarkDotNet.Artifacts/ |  | ||||||
|  |  | ||||||
| # .NET Core |  | ||||||
| project.lock.json |  | ||||||
| project.fragment.lock.json |  | ||||||
| artifacts/ |  | ||||||
|  |  | ||||||
| # StyleCop |  | ||||||
| StyleCopReport.xml |  | ||||||
|  |  | ||||||
| # Files built by Visual Studio |  | ||||||
| *_i.c |  | ||||||
| *_p.c |  | ||||||
| *_h.h |  | ||||||
| *.ilk |  | ||||||
| *.meta |  | ||||||
| *.obj |  | ||||||
| *.iobj |  | ||||||
| *.pch |  | ||||||
| *.pdb |  | ||||||
| *.ipdb |  | ||||||
| *.pgc |  | ||||||
| *.pgd |  | ||||||
| *.rsp |  | ||||||
| *.sbr |  | ||||||
| *.tlb |  | ||||||
| *.tli |  | ||||||
| *.tlh |  | ||||||
| *.tmp |  | ||||||
| *.tmp_proj |  | ||||||
| *_wpftmp.csproj |  | ||||||
| *.log |  | ||||||
| *.vspscc |  | ||||||
| *.vssscc |  | ||||||
| .builds |  | ||||||
| *.pidb |  | ||||||
| *.svclog |  | ||||||
| *.scc |  | ||||||
|  |  | ||||||
| # Chutzpah Test files |  | ||||||
| _Chutzpah* |  | ||||||
|  |  | ||||||
| # Visual C++ cache files |  | ||||||
| ipch/ |  | ||||||
| *.aps |  | ||||||
| *.ncb |  | ||||||
| *.opendb |  | ||||||
| *.opensdf |  | ||||||
| *.sdf |  | ||||||
| *.cachefile |  | ||||||
| *.VC.db |  | ||||||
| *.VC.VC.opendb |  | ||||||
|  |  | ||||||
| # Visual Studio profiler |  | ||||||
| *.psess |  | ||||||
| *.vsp |  | ||||||
| *.vspx |  | ||||||
| *.sap |  | ||||||
|  |  | ||||||
| # Visual Studio Trace Files |  | ||||||
| *.e2e |  | ||||||
|  |  | ||||||
| # TFS 2012 Local Workspace |  | ||||||
| $tf/ |  | ||||||
|  |  | ||||||
| # Guidance Automation Toolkit |  | ||||||
| *.gpState |  | ||||||
|  |  | ||||||
| # ReSharper is a .NET coding add-in |  | ||||||
| _ReSharper*/ |  | ||||||
| *.[Rr]e[Ss]harper |  | ||||||
| *.DotSettings.user |  | ||||||
|  |  | ||||||
| # JustCode is a .NET coding add-in |  | ||||||
| .JustCode |  | ||||||
|  |  | ||||||
| # TeamCity is a build add-in |  | ||||||
| _TeamCity* |  | ||||||
|  |  | ||||||
| # DotCover is a Code Coverage Tool |  | ||||||
| *.dotCover |  | ||||||
|  |  | ||||||
| # AxoCover is a Code Coverage Tool |  | ||||||
| .axoCover/* |  | ||||||
| !.axoCover/settings.json |  | ||||||
|  |  | ||||||
| # Visual Studio code coverage results |  | ||||||
| *.coverage |  | ||||||
| *.coveragexml |  | ||||||
|  |  | ||||||
| # NCrunch |  | ||||||
| _NCrunch_* |  | ||||||
| .*crunch*.local.xml |  | ||||||
| nCrunchTemp_* |  | ||||||
| .ncrunchsolution |  | ||||||
|  |  | ||||||
| # MightyMoose |  | ||||||
| *.mm.* |  | ||||||
| AutoTest.Net/ |  | ||||||
|  |  | ||||||
| # Web workbench (sass) |  | ||||||
| .sass-cache/ |  | ||||||
|  |  | ||||||
| # Installshield output folder |  | ||||||
| [Ee]xpress/ |  | ||||||
|  |  | ||||||
| # DocProject is a documentation generator add-in |  | ||||||
| DocProject/buildhelp/ |  | ||||||
| DocProject/Help/*.HxT |  | ||||||
| DocProject/Help/*.HxC |  | ||||||
| DocProject/Help/*.hhc |  | ||||||
| DocProject/Help/*.hhk |  | ||||||
| DocProject/Help/*.hhp |  | ||||||
| DocProject/Help/Html2 |  | ||||||
| DocProject/Help/html |  | ||||||
|  |  | ||||||
| # Click-Once directory |  | ||||||
| publish/ |  | ||||||
|  |  | ||||||
| # Publish Web Output |  | ||||||
| *.[Pp]ublish.xml |  | ||||||
| *.azurePubxml |  | ||||||
| # Note: Comment the next line if you want to checkin your web deploy settings, |  | ||||||
| # but database connection strings (with potential passwords) will be unencrypted |  | ||||||
| *.pubxml |  | ||||||
| *.publishproj |  | ||||||
|  |  | ||||||
| # Microsoft Azure Web App publish settings. Comment the next line if you want to |  | ||||||
| # checkin your Azure Web App publish settings, but sensitive information contained |  | ||||||
| # in these scripts will be unencrypted |  | ||||||
| PublishScripts/ |  | ||||||
|  |  | ||||||
| # NuGet Packages |  | ||||||
| *.nupkg |  | ||||||
| # The packages folder can be ignored because of Package Restore |  | ||||||
| **/[Pp]ackages/* |  | ||||||
| # except build/, which is used as an MSBuild target. |  | ||||||
| !**/[Pp]ackages/build/ |  | ||||||
| # Uncomment if necessary however generally it will be regenerated when needed |  | ||||||
| #!**/[Pp]ackages/repositories.config |  | ||||||
| # NuGet v3's project.json files produces more ignorable files |  | ||||||
| *.nuget.props |  | ||||||
| *.nuget.targets |  | ||||||
|  |  | ||||||
| # Microsoft Azure Build Output |  | ||||||
| csx/ |  | ||||||
| *.build.csdef |  | ||||||
|  |  | ||||||
| # Microsoft Azure Emulator |  | ||||||
| ecf/ |  | ||||||
| rcf/ |  | ||||||
|  |  | ||||||
| # Windows Store app package directories and files |  | ||||||
| AppPackages/ |  | ||||||
| BundleArtifacts/ |  | ||||||
| Package.StoreAssociation.xml |  | ||||||
| _pkginfo.txt |  | ||||||
| *.appx |  | ||||||
|  |  | ||||||
| # Visual Studio cache files |  | ||||||
| # files ending in .cache can be ignored |  | ||||||
| *.[Cc]ache |  | ||||||
| # but keep track of directories ending in .cache |  | ||||||
| !?*.[Cc]ache/ |  | ||||||
|  |  | ||||||
| # Others |  | ||||||
| ClientBin/ |  | ||||||
| ~$* |  | ||||||
| *~ |  | ||||||
| *.dbmdl |  | ||||||
| *.dbproj.schemaview |  | ||||||
| *.jfm |  | ||||||
| *.pfx |  | ||||||
| *.publishsettings |  | ||||||
| orleans.codegen.cs |  | ||||||
|  |  | ||||||
| # Including strong name files can present a security risk |  | ||||||
| # (https://github.com/github/gitignore/pull/2483#issue-259490424) |  | ||||||
| #*.snk |  | ||||||
|  |  | ||||||
| # Since there are multiple workflows, uncomment next line to ignore bower_components |  | ||||||
| # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) |  | ||||||
| #bower_components/ |  | ||||||
|  |  | ||||||
| # RIA/Silverlight projects |  | ||||||
| Generated_Code/ |  | ||||||
|  |  | ||||||
| # Backup & report files from converting an old project file |  | ||||||
| # to a newer Visual Studio version. Backup files are not needed, |  | ||||||
| # because we have git ;-) |  | ||||||
| _UpgradeReport_Files/ |  | ||||||
| Backup*/ |  | ||||||
| UpgradeLog*.XML |  | ||||||
| UpgradeLog*.htm |  | ||||||
| ServiceFabricBackup/ |  | ||||||
| *.rptproj.bak |  | ||||||
|  |  | ||||||
| # SQL Server files |  | ||||||
| *.mdf |  | ||||||
| *.ldf |  | ||||||
| *.ndf |  | ||||||
|  |  | ||||||
| # Business Intelligence projects |  | ||||||
| *.rdl.data |  | ||||||
| *.bim.layout |  | ||||||
| *.bim_*.settings |  | ||||||
| *.rptproj.rsuser |  | ||||||
| *- Backup*.rdl |  | ||||||
|  |  | ||||||
| # Microsoft Fakes |  | ||||||
| FakesAssemblies/ |  | ||||||
|  |  | ||||||
| # GhostDoc plugin setting file |  | ||||||
| *.GhostDoc.xml |  | ||||||
|  |  | ||||||
| # Node.js Tools for Visual Studio |  | ||||||
| .ntvs_analysis.dat |  | ||||||
| node_modules/ |  | ||||||
|  |  | ||||||
| # Visual Studio 6 build log |  | ||||||
| *.plg |  | ||||||
|  |  | ||||||
| # Visual Studio 6 workspace options file |  | ||||||
| *.opt |  | ||||||
|  |  | ||||||
| # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) |  | ||||||
| *.vbw |  | ||||||
|  |  | ||||||
| # Visual Studio LightSwitch build output |  | ||||||
| **/*.HTMLClient/GeneratedArtifacts |  | ||||||
| **/*.DesktopClient/GeneratedArtifacts |  | ||||||
| **/*.DesktopClient/ModelManifest.xml |  | ||||||
| **/*.Server/GeneratedArtifacts |  | ||||||
| **/*.Server/ModelManifest.xml |  | ||||||
| _Pvt_Extensions |  | ||||||
|  |  | ||||||
| # Paket dependency manager |  | ||||||
| .paket/paket.exe |  | ||||||
| paket-files/ |  | ||||||
|  |  | ||||||
| # FAKE - F# Make |  | ||||||
| .fake/ |  | ||||||
|  |  | ||||||
| # JetBrains Rider |  | ||||||
| .idea/ |  | ||||||
| *.sln.iml |  | ||||||
|  |  | ||||||
| # CodeRush personal settings |  | ||||||
| .cr/personal |  | ||||||
|  |  | ||||||
| # Python Tools for Visual Studio (PTVS) |  | ||||||
| __pycache__/ |  | ||||||
| *.pyc |  | ||||||
|  |  | ||||||
| # Cake - Uncomment if you are using it |  | ||||||
| # tools/** |  | ||||||
| # !tools/packages.config |  | ||||||
|  |  | ||||||
| # Tabs Studio |  | ||||||
| *.tss |  | ||||||
|  |  | ||||||
| # Telerik's JustMock configuration file |  | ||||||
| *.jmconfig |  | ||||||
|  |  | ||||||
| # BizTalk build output |  | ||||||
| *.btp.cs |  | ||||||
| *.btm.cs |  | ||||||
| *.odx.cs |  | ||||||
| *.xsd.cs |  | ||||||
|  |  | ||||||
| # OpenCover UI analysis results |  | ||||||
| OpenCover/ |  | ||||||
|  |  | ||||||
| # Azure Stream Analytics local run output |  | ||||||
| ASALocalRun/ |  | ||||||
|  |  | ||||||
| # MSBuild Binary and Structured Log |  | ||||||
| *.binlog |  | ||||||
|  |  | ||||||
| # NVidia Nsight GPU debugger configuration file |  | ||||||
| *.nvuser |  | ||||||
|  |  | ||||||
| # MFractors (Xamarin productivity tool) working folder |  | ||||||
| .mfractor/ |  | ||||||
|  |  | ||||||
| # Local History for Visual Studio |  | ||||||
| .localhistory/ |  | ||||||
|  |  | ||||||
| # BeatPulse healthcheck temp database |  | ||||||
| healthchecksdb |  | ||||||
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 20 KiB | 
							
								
								
									
										24
									
								
								Changelog.md
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								Changelog.md
									
									
									
									
									
								
							| @@ -1,24 +0,0 @@ | |||||||
| ### v1.3 (23-May-2020) |  | ||||||
|  |  | ||||||
| - Changed analyzers to report errors instead of warnings. If you find that some analyzer works incorrectly, please report it on GitHub. You can also configure inspection severity overrides in your project if you need to. |  | ||||||
| - Improved help text by showing default values for non-required options. This only works on types that have a custom override for `ToString()` method. Additionally, if the type implements `IFormattable`, the overload with a format provider will be used instead. (Thanks [@Domn Werner](https://github.com/domn1995)) |  | ||||||
| - Changed default version text to only show 3 version components instead of 4, if the last component (revision) is not specified or is zero. This makes the default version text compliant with semantic versioning. |  | ||||||
| - Fixed an issue where it was possible to define a command with an option that has the same name or short name as built-in help or version options. Previously it would lead to the user-defined option being ignored in favor of the built-in option. Now this will throw an exception instead. |  | ||||||
| - Changed the underlying representation of `StreamReader`/`StreamWriter` objects used in `SystemConsole` and `VirtualConsole` to be thread-safe. |  | ||||||
|  |  | ||||||
| ### v1.2 (11-May-2020) |  | ||||||
|  |  | ||||||
| - Added built-in Roslyn analyzers that help catch incorrect usage of the library. Currently, all analyzers report issues as warnings so as to not prevent the project from building. In the future that may change. |  | ||||||
| - Added an optional parameter to `new CommandException(...)` called `showHelp` which can be used to instruct CliFx to show help for the current command after printing the error. (Thanks [@Domn Werner](https://github.com/domn1995)) |  | ||||||
| - Improved help text shown for enum options and parameters by providing the list of valid values that the enum can accept. (Thanks [@Domn Werner](https://github.com/domn1995)) |  | ||||||
| - Fixed an issue where it was possible to set an option without providing a value, while the option was marked as required. |  | ||||||
| - Fixed an issue where it was possible to configure an option with an empty name or a name consisting of a single character. If you want to use a single character as a name, you should set the option's short name instead. |  | ||||||
| - Added `CursorLeft` and `CursorTop` properties to `IConsole` and its implementations. In `VirtualConsole`, these are just auto-properties. |  | ||||||
| - Improved exception messages. |  | ||||||
| - Improved exceptions related to user input by also showing help text after the error message. (Thanks [@Domn Werner](https://github.com/domn1995)) |  | ||||||
|  |  | ||||||
| ### v1.1 (16-Mar-2020) |  | ||||||
|  |  | ||||||
| - Changed `IConsole` interface (and as a result, `SystemConsole` and `VirtualConsole`) to support writing binary data. Instead of `TextReader`/`TextWriter` instances, the streams are now exposed as `StreamReader`/`StreamWriter` which provide the `BaseStream` property that allows raw access. Existing usages inside commands should remain the same because `StreamReader`/`StreamWriter` are compatible with their base classes `TextReader`/`TextWriter`, but if you were using `VirtualConsole` in tests, you may have to update it to the new API. Refer to the readme for more info. |  | ||||||
| - Changed argument binding behavior so that an error is produced if the user provides an argument that doesn't match with any parameter or option. This is done in order to improve user experience, as otherwise the user may make a typo without knowing that their input wasn't taken into account. |  | ||||||
| - Changed argument binding behavior so that options can be set to multiple argument values while specifying them with mixed naming. For example, `--option value1 -o value2 --option value3` would result in the option being set to corresponding three values, assuming `--option` and `-o` match with the same option. |  | ||||||
| @@ -1,43 +0,0 @@ | |||||||
| using System.Collections.Generic; |  | ||||||
| using System.Linq; |  | ||||||
| using Microsoft.CodeAnalysis; |  | ||||||
|  |  | ||||||
| namespace CliFx.Analyzers.Tests |  | ||||||
| { |  | ||||||
|     public class AnalyzerTestCase |  | ||||||
|     { |  | ||||||
|         public string Name { get; } |  | ||||||
|  |  | ||||||
|         public IReadOnlyList<DiagnosticDescriptor> TestedDiagnostics { get; } |  | ||||||
|  |  | ||||||
|         public IReadOnlyList<string> SourceCodes { get; } |  | ||||||
|  |  | ||||||
|         public AnalyzerTestCase( |  | ||||||
|             string name, |  | ||||||
|             IReadOnlyList<DiagnosticDescriptor> testedDiagnostics, |  | ||||||
|             IReadOnlyList<string> sourceCodes) |  | ||||||
|         { |  | ||||||
|             Name = name; |  | ||||||
|             TestedDiagnostics = testedDiagnostics; |  | ||||||
|             SourceCodes = sourceCodes; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public AnalyzerTestCase( |  | ||||||
|             string name, |  | ||||||
|             IReadOnlyList<DiagnosticDescriptor> testedDiagnostics, |  | ||||||
|             string sourceCode) |  | ||||||
|             : this(name, testedDiagnostics, new[] {sourceCode}) |  | ||||||
|         { |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public AnalyzerTestCase( |  | ||||||
|             string name, |  | ||||||
|             DiagnosticDescriptor testedDiagnostic, |  | ||||||
|             string sourceCode) |  | ||||||
|             : this(name, new[] {testedDiagnostic}, sourceCode) |  | ||||||
|         { |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public override string ToString() => $"{Name} [{string.Join(", ", TestedDiagnostics.Select(d => d.Id))}]"; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,29 +0,0 @@ | |||||||
| <Project Sdk="Microsoft.NET.Sdk"> |  | ||||||
|   <Import Project="../CliFx.props" /> |  | ||||||
|  |  | ||||||
|   <PropertyGroup> |  | ||||||
|     <TargetFramework>netcoreapp3.1</TargetFramework> |  | ||||||
|     <IsPackable>false</IsPackable> |  | ||||||
|     <IsTestProject>true</IsTestProject> |  | ||||||
|     <CollectCoverage>true</CollectCoverage> |  | ||||||
|     <CoverletOutputFormat>opencover</CoverletOutputFormat> |  | ||||||
|     <CoverletOutput>bin/$(Configuration)/Coverage.xml</CoverletOutput> |  | ||||||
|   </PropertyGroup> |  | ||||||
|  |  | ||||||
|   <ItemGroup> |  | ||||||
|     <PackageReference Include="Gu.Roslyn.Asserts" Version="3.3.0" /> |  | ||||||
|     <PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" /> |  | ||||||
|     <PackageReference Include="FluentAssertions" Version="5.10.2" /> |  | ||||||
|     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> |  | ||||||
|     <PackageReference Include="xunit" Version="2.4.0" /> |  | ||||||
|     <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> |  | ||||||
|     <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" /> |  | ||||||
|     <PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" /> |  | ||||||
|   </ItemGroup> |  | ||||||
|  |  | ||||||
|   <ItemGroup> |  | ||||||
|     <ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" /> |  | ||||||
|     <ProjectReference Include="..\CliFx\CliFx.csproj" /> |  | ||||||
|   </ItemGroup> |  | ||||||
|  |  | ||||||
| </Project> |  | ||||||
| @@ -1,489 +0,0 @@ | |||||||
| using System.Collections.Generic; |  | ||||||
| using CliFx.Analyzers.Tests.Internal; |  | ||||||
| using Microsoft.CodeAnalysis.Diagnostics; |  | ||||||
| using Xunit; |  | ||||||
|  |  | ||||||
| namespace CliFx.Analyzers.Tests |  | ||||||
| { |  | ||||||
|     public class CommandSchemaAnalyzerTests |  | ||||||
|     { |  | ||||||
|         private static DiagnosticAnalyzer Analyzer { get; } = new CommandSchemaAnalyzer(); |  | ||||||
|  |  | ||||||
|         public static IEnumerable<object[]> GetValidCases() |  | ||||||
|         { |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Non-command type", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| public class Foo |  | ||||||
| { |  | ||||||
|     public int Bar { get; set; } = 5; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Command implements interface and has attribute", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Command doesn't have an attribute but is an abstract type", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| public abstract class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Parameters with unique order", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandParameter(13)] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandParameter(15)] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Parameters with unique names", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandParameter(13, Name = ""foo"")] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandParameter(15, Name = ""bar"")] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Single non-scalar parameter", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandParameter(1)] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandParameter(2)] |  | ||||||
|     public HashSet<string> ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Non-scalar parameter is last in order", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandParameter(1)] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandParameter(2)] |  | ||||||
|     public IReadOnlyList<string> ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Option with a proper name", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption(""foo"")] |  | ||||||
|     public string Param { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Option with a proper name and short name", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption(""foo"", 'f')] |  | ||||||
|     public string Param { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Options with unique names", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption(""foo"")] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandOption(""bar"")] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Options with unique short names", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption('f')] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandOption('x')] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Options with unique environment variable names", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption('a', EnvironmentVariableName = ""env_var_a"")] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandOption('b', EnvironmentVariableName = ""env_var_b"")] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static IEnumerable<object[]> GetInvalidCases() |  | ||||||
|         { |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Command is missing the attribute", |  | ||||||
|                     DiagnosticDescriptors.CliFx0002, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Command doesn't implement the interface", |  | ||||||
|                     DiagnosticDescriptors.CliFx0001, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand |  | ||||||
| { |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Parameters with duplicate order", |  | ||||||
|                     DiagnosticDescriptors.CliFx0021, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandParameter(13)] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandParameter(13)] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Parameters with duplicate names", |  | ||||||
|                     DiagnosticDescriptors.CliFx0022, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandParameter(13, Name = ""foo"")] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandParameter(15, Name = ""foo"")] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Multiple non-scalar parameters", |  | ||||||
|                     DiagnosticDescriptors.CliFx0023, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandParameter(1)] |  | ||||||
|     public IReadOnlyList<string> ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandParameter(2)] |  | ||||||
|     public HashSet<string> ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Non-last non-scalar parameter", |  | ||||||
|                     DiagnosticDescriptors.CliFx0024, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandParameter(1)] |  | ||||||
|     public IReadOnlyList<string> ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandParameter(2)] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Option with an empty name", |  | ||||||
|                     DiagnosticDescriptors.CliFx0041, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption("""")] |  | ||||||
|     public string Param { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Option with a name which is too short", |  | ||||||
|                     DiagnosticDescriptors.CliFx0042, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption(""a"")] |  | ||||||
|     public string Param { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Options with duplicate names", |  | ||||||
|                     DiagnosticDescriptors.CliFx0043, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption(""foo"")] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandOption(""foo"")] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Options with duplicate short names", |  | ||||||
|                     DiagnosticDescriptors.CliFx0044, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption('f')] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandOption('f')] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Options with duplicate environment variable names", |  | ||||||
|                     DiagnosticDescriptors.CliFx0045, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     [CommandOption('a', EnvironmentVariableName = ""env_var"")] |  | ||||||
|     public string ParamA { get; set; } |  | ||||||
|      |  | ||||||
|     [CommandOption('b', EnvironmentVariableName = ""env_var"")] |  | ||||||
|     public string ParamB { get; set; } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Theory] |  | ||||||
|         [MemberData(nameof(GetValidCases))] |  | ||||||
|         public void Valid(AnalyzerTestCase testCase) => |  | ||||||
|             Analyzer.Should().NotProduceDiagnostics(testCase); |  | ||||||
|  |  | ||||||
|         [Theory] |  | ||||||
|         [MemberData(nameof(GetInvalidCases))] |  | ||||||
|         public void Invalid(AnalyzerTestCase testCase) => |  | ||||||
|             Analyzer.Should().ProduceDiagnostics(testCase); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,144 +0,0 @@ | |||||||
| using System.Collections.Generic; |  | ||||||
| using CliFx.Analyzers.Tests.Internal; |  | ||||||
| using Microsoft.CodeAnalysis.Diagnostics; |  | ||||||
| using Xunit; |  | ||||||
|  |  | ||||||
| namespace CliFx.Analyzers.Tests |  | ||||||
| { |  | ||||||
|     public class ConsoleUsageAnalyzerTests |  | ||||||
|     { |  | ||||||
|         private static DiagnosticAnalyzer Analyzer { get; } = new ConsoleUsageAnalyzer(); |  | ||||||
|  |  | ||||||
|         public static IEnumerable<object[]> GetValidCases() |  | ||||||
|         { |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Using console abstraction", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) |  | ||||||
|     { |  | ||||||
|         console.Output.WriteLine(""Hello world""); |  | ||||||
|         return default; |  | ||||||
|     } |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Console abstraction is not available in scope", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     public void SomeOtherMethod() => Console.WriteLine(""Test""); |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static IEnumerable<object[]> GetInvalidCases() |  | ||||||
|         { |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Not using available console abstraction in the ExecuteAsync method", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) |  | ||||||
|     { |  | ||||||
|         Console.WriteLine(""Hello world""); |  | ||||||
|         return default; |  | ||||||
|     } |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Not using available console abstraction in the ExecuteAsync method when writing stderr", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) |  | ||||||
|     { |  | ||||||
|         Console.Error.WriteLine(""Hello world""); |  | ||||||
|         return default; |  | ||||||
|     } |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Not using available console abstraction while referencing System.Console by full name", |  | ||||||
|                     Analyzer.SupportedDiagnostics, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) |  | ||||||
|     { |  | ||||||
|         System.Console.Error.WriteLine(""Hello world""); |  | ||||||
|         return default; |  | ||||||
|     } |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             yield return new object[] |  | ||||||
|             { |  | ||||||
|                 new AnalyzerTestCase( |  | ||||||
|                     "Not using available console abstraction in another method", |  | ||||||
|                     DiagnosticDescriptors.CliFx0100, |  | ||||||
|  |  | ||||||
|                     // language=cs |  | ||||||
|                     @" |  | ||||||
| [Command] |  | ||||||
| public class MyCommand : ICommand |  | ||||||
| { |  | ||||||
|     public void SomeOtherMethod(IConsole console) => Console.WriteLine(""Test""); |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
| }" |  | ||||||
|                 ) |  | ||||||
|             }; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Theory] |  | ||||||
|         [MemberData(nameof(GetValidCases))] |  | ||||||
|         public void Valid(AnalyzerTestCase testCase) => |  | ||||||
|             Analyzer.Should().NotProduceDiagnostics(testCase); |  | ||||||
|  |  | ||||||
|         [Theory] |  | ||||||
|         [MemberData(nameof(GetInvalidCases))] |  | ||||||
|         public void Invalid(AnalyzerTestCase testCase) => |  | ||||||
|             Analyzer.Should().ProduceDiagnostics(testCase); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,107 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Linq; |  | ||||||
| using FluentAssertions.Execution; |  | ||||||
| using FluentAssertions.Primitives; |  | ||||||
| using Gu.Roslyn.Asserts; |  | ||||||
| using Microsoft.CodeAnalysis; |  | ||||||
| using Microsoft.CodeAnalysis.CSharp; |  | ||||||
| using Microsoft.CodeAnalysis.Diagnostics; |  | ||||||
|  |  | ||||||
| namespace CliFx.Analyzers.Tests.Internal |  | ||||||
| { |  | ||||||
|     internal partial class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer, AnalyzerAssertions> |  | ||||||
|     { |  | ||||||
|         protected override string Identifier { get; } = "analyzer"; |  | ||||||
|  |  | ||||||
|         public AnalyzerAssertions(DiagnosticAnalyzer analyzer) |  | ||||||
|             : base(analyzer) |  | ||||||
|         { |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public void ProduceDiagnostics( |  | ||||||
|             IReadOnlyList<DiagnosticDescriptor> diagnostics, |  | ||||||
|             IReadOnlyList<string> sourceCodes) |  | ||||||
|         { |  | ||||||
|             var producedDiagnostics = GetProducedDiagnostics(Subject, sourceCodes); |  | ||||||
|  |  | ||||||
|             var expectedIds = diagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray(); |  | ||||||
|             var producedIds = producedDiagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray(); |  | ||||||
|  |  | ||||||
|             var result = expectedIds.Intersect(producedIds).Count() == expectedIds.Length; |  | ||||||
|  |  | ||||||
|             Execute.Assertion.ForCondition(result).FailWith($@" |  | ||||||
| Expected and produced diagnostics do not match. |  | ||||||
|  |  | ||||||
| Expected: {string.Join(", ", expectedIds)} |  | ||||||
| Produced: {(producedIds.Any() ? string.Join(", ", producedIds) : "<none>")} |  | ||||||
| ".Trim()); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public void ProduceDiagnostics(AnalyzerTestCase testCase) => |  | ||||||
|             ProduceDiagnostics(testCase.TestedDiagnostics, testCase.SourceCodes); |  | ||||||
|  |  | ||||||
|         public void NotProduceDiagnostics( |  | ||||||
|             IReadOnlyList<DiagnosticDescriptor> diagnostics, |  | ||||||
|             IReadOnlyList<string> sourceCodes) |  | ||||||
|         { |  | ||||||
|             var producedDiagnostics = GetProducedDiagnostics(Subject, sourceCodes); |  | ||||||
|  |  | ||||||
|             var expectedIds = diagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray(); |  | ||||||
|             var producedIds = producedDiagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray(); |  | ||||||
|  |  | ||||||
|             var result = !expectedIds.Intersect(producedIds).Any(); |  | ||||||
|  |  | ||||||
|             Execute.Assertion.ForCondition(result).FailWith($@" |  | ||||||
| Expected no produced diagnostics. |  | ||||||
|  |  | ||||||
| Produced: {string.Join(", ", producedIds)} |  | ||||||
| ".Trim()); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public void NotProduceDiagnostics(AnalyzerTestCase testCase) => |  | ||||||
|             NotProduceDiagnostics(testCase.TestedDiagnostics, testCase.SourceCodes); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     internal partial class AnalyzerAssertions |  | ||||||
|     { |  | ||||||
|         private static IReadOnlyList<MetadataReference> DefaultMetadataReferences { get; } = |  | ||||||
|             MetadataReferences.Transitive(typeof(CliApplication).Assembly).ToArray(); |  | ||||||
|  |  | ||||||
|         private static string WrapCodeWithUsingDirectives(string code) |  | ||||||
|         { |  | ||||||
|             var usingDirectives = new[] |  | ||||||
|             { |  | ||||||
|                 "using System;", |  | ||||||
|                 "using System.Collections.Generic;", |  | ||||||
|                 "using System.Threading.Tasks;", |  | ||||||
|                 "using CliFx;", |  | ||||||
|                 "using CliFx.Attributes;", |  | ||||||
|                 "using CliFx.Exceptions;", |  | ||||||
|                 "using CliFx.Utilities;" |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             return |  | ||||||
|                 string.Join(Environment.NewLine, usingDirectives) + |  | ||||||
|                 Environment.NewLine + |  | ||||||
|                 code; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private static IReadOnlyList<Diagnostic> GetProducedDiagnostics( |  | ||||||
|             DiagnosticAnalyzer analyzer, |  | ||||||
|             IReadOnlyList<string> sourceCodes) |  | ||||||
|         { |  | ||||||
|             var compilationOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication); |  | ||||||
|             var wrappedSourceCodes = sourceCodes.Select(WrapCodeWithUsingDirectives).ToArray(); |  | ||||||
|  |  | ||||||
|             return Analyze.GetDiagnostics(analyzer, wrappedSourceCodes, compilationOptions, DefaultMetadataReferences) |  | ||||||
|                 .SelectMany(d => d) |  | ||||||
|                 .ToArray(); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     internal static class AnalyzerAssertionsExtensions |  | ||||||
|     { |  | ||||||
|         public static AnalyzerAssertions Should(this DiagnosticAnalyzer analyzer) => new AnalyzerAssertions(analyzer); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,13 +0,0 @@ | |||||||
| <Project Sdk="Microsoft.NET.Sdk"> |  | ||||||
|   <Import Project="../CliFx.props" /> |  | ||||||
|  |  | ||||||
|   <PropertyGroup> |  | ||||||
|     <TargetFramework>netstandard2.0</TargetFramework> |  | ||||||
|     <Nullable>annotations</Nullable> |  | ||||||
|   </PropertyGroup> |  | ||||||
|  |  | ||||||
|   <ItemGroup> |  | ||||||
|     <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" PrivateAssets="all" /> |  | ||||||
|   </ItemGroup> |  | ||||||
|  |  | ||||||
| </Project> |  | ||||||
| @@ -1,297 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Collections.Immutable; |  | ||||||
| using System.Linq; |  | ||||||
| using Microsoft.CodeAnalysis; |  | ||||||
| using Microsoft.CodeAnalysis.Diagnostics; |  | ||||||
|  |  | ||||||
| namespace CliFx.Analyzers |  | ||||||
| { |  | ||||||
|     [DiagnosticAnalyzer(LanguageNames.CSharp)] |  | ||||||
|     public class CommandSchemaAnalyzer : DiagnosticAnalyzer |  | ||||||
|     { |  | ||||||
|         public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create( |  | ||||||
|             DiagnosticDescriptors.CliFx0001, |  | ||||||
|             DiagnosticDescriptors.CliFx0002, |  | ||||||
|             DiagnosticDescriptors.CliFx0021, |  | ||||||
|             DiagnosticDescriptors.CliFx0022, |  | ||||||
|             DiagnosticDescriptors.CliFx0023, |  | ||||||
|             DiagnosticDescriptors.CliFx0024, |  | ||||||
|             DiagnosticDescriptors.CliFx0041, |  | ||||||
|             DiagnosticDescriptors.CliFx0042, |  | ||||||
|             DiagnosticDescriptors.CliFx0043, |  | ||||||
|             DiagnosticDescriptors.CliFx0044, |  | ||||||
|             DiagnosticDescriptors.CliFx0045 |  | ||||||
|         ); |  | ||||||
|  |  | ||||||
|         private static bool IsScalarType(ITypeSymbol typeSymbol) => |  | ||||||
|             KnownSymbols.IsSystemString(typeSymbol) || |  | ||||||
|             !typeSymbol.AllInterfaces.Select(i => i.ConstructedFrom).Any(KnownSymbols.IsSystemCollectionsGenericIEnumerable); |  | ||||||
|  |  | ||||||
|         private static void CheckCommandParameterProperties( |  | ||||||
|             SymbolAnalysisContext context, |  | ||||||
|             IReadOnlyList<IPropertySymbol> properties) |  | ||||||
|         { |  | ||||||
|             var parameters = properties |  | ||||||
|                 .Select(p => |  | ||||||
|                 { |  | ||||||
|                     var attribute = p |  | ||||||
|                         .GetAttributes() |  | ||||||
|                         .First(a => KnownSymbols.IsCommandParameterAttribute(a.AttributeClass)); |  | ||||||
|  |  | ||||||
|                     var order = attribute |  | ||||||
|                         .ConstructorArguments |  | ||||||
|                         .Select(a => a.Value) |  | ||||||
|                         .FirstOrDefault() as int?; |  | ||||||
|  |  | ||||||
|                     var name = attribute |  | ||||||
|                         .NamedArguments |  | ||||||
|                         .Where(a => a.Key == "Name") |  | ||||||
|                         .Select(a => a.Value.Value) |  | ||||||
|                         .FirstOrDefault() as string; |  | ||||||
|  |  | ||||||
|                     return new |  | ||||||
|                     { |  | ||||||
|                         Property = p, |  | ||||||
|                         Order = order, |  | ||||||
|                         Name = name |  | ||||||
|                     }; |  | ||||||
|                 }) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             // Duplicate order |  | ||||||
|             var duplicateOrderParameters = parameters |  | ||||||
|                 .Where(p => p.Order != null) |  | ||||||
|                 .GroupBy(p => p.Order) |  | ||||||
|                 .Where(g => g.Count() > 1) |  | ||||||
|                 .SelectMany(g => g.AsEnumerable()) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             foreach (var parameter in duplicateOrderParameters) |  | ||||||
|             { |  | ||||||
|                 context.ReportDiagnostic( |  | ||||||
|                     Diagnostic.Create(DiagnosticDescriptors.CliFx0021, parameter.Property.Locations.First())); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // Duplicate name |  | ||||||
|             var duplicateNameParameters = parameters |  | ||||||
|                 .Where(p => !string.IsNullOrWhiteSpace(p.Name)) |  | ||||||
|                 .GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase) |  | ||||||
|                 .Where(g => g.Count() > 1) |  | ||||||
|                 .SelectMany(g => g.AsEnumerable()) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             foreach (var parameter in duplicateNameParameters) |  | ||||||
|             { |  | ||||||
|                 context.ReportDiagnostic( |  | ||||||
|                     Diagnostic.Create(DiagnosticDescriptors.CliFx0022, parameter.Property.Locations.First())); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // Multiple non-scalar |  | ||||||
|             var nonScalarParameters = parameters |  | ||||||
|                 .Where(p => !IsScalarType(p.Property.Type)) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             if (nonScalarParameters.Length > 1) |  | ||||||
|             { |  | ||||||
|                 foreach (var parameter in nonScalarParameters) |  | ||||||
|                 { |  | ||||||
|                     context.ReportDiagnostic( |  | ||||||
|                         Diagnostic.Create(DiagnosticDescriptors.CliFx0023, parameter.Property.Locations.First())); |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // Non-last non-scalar |  | ||||||
|             var nonLastNonScalarParameter = parameters |  | ||||||
|                 .OrderByDescending(a => a.Order) |  | ||||||
|                 .Skip(1) |  | ||||||
|                 .LastOrDefault(p => !IsScalarType(p.Property.Type)); |  | ||||||
|  |  | ||||||
|             if (nonLastNonScalarParameter != null) |  | ||||||
|             { |  | ||||||
|                 context.ReportDiagnostic( |  | ||||||
|                     Diagnostic.Create(DiagnosticDescriptors.CliFx0024, nonLastNonScalarParameter.Property.Locations.First())); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private static void CheckCommandOptionProperties( |  | ||||||
|             SymbolAnalysisContext context, |  | ||||||
|             IReadOnlyList<IPropertySymbol> properties) |  | ||||||
|         { |  | ||||||
|             var options = properties |  | ||||||
|                 .Select(p => |  | ||||||
|                 { |  | ||||||
|                     var attribute = p |  | ||||||
|                         .GetAttributes() |  | ||||||
|                         .First(a => KnownSymbols.IsCommandOptionAttribute(a.AttributeClass)); |  | ||||||
|  |  | ||||||
|                     var name = attribute |  | ||||||
|                         .ConstructorArguments |  | ||||||
|                         .Where(a => KnownSymbols.IsSystemString(a.Type)) |  | ||||||
|                         .Select(a => a.Value) |  | ||||||
|                         .FirstOrDefault() as string; |  | ||||||
|  |  | ||||||
|                     var shortName = attribute |  | ||||||
|                         .ConstructorArguments |  | ||||||
|                         .Where(a => KnownSymbols.IsSystemChar(a.Type)) |  | ||||||
|                         .Select(a => a.Value) |  | ||||||
|                         .FirstOrDefault() as char?; |  | ||||||
|  |  | ||||||
|                     var envVarName = attribute |  | ||||||
|                         .NamedArguments |  | ||||||
|                         .Where(a => a.Key == "EnvironmentVariableName") |  | ||||||
|                         .Select(a => a.Value.Value) |  | ||||||
|                         .FirstOrDefault() as string; |  | ||||||
|  |  | ||||||
|                     return new |  | ||||||
|                     { |  | ||||||
|                         Property = p, |  | ||||||
|                         Name = name, |  | ||||||
|                         ShortName = shortName, |  | ||||||
|                         EnvironmentVariableName = envVarName |  | ||||||
|                     }; |  | ||||||
|                 }) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             // No name |  | ||||||
|             var noNameOptions = options |  | ||||||
|                 .Where(o => string.IsNullOrWhiteSpace(o.Name) && o.ShortName == null) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             foreach (var option in noNameOptions) |  | ||||||
|             { |  | ||||||
|                 context.ReportDiagnostic( |  | ||||||
|                     Diagnostic.Create(DiagnosticDescriptors.CliFx0041, option.Property.Locations.First())); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // Too short name |  | ||||||
|             var invalidNameLengthOptions = options |  | ||||||
|                 .Where(o => !string.IsNullOrWhiteSpace(o.Name) && o.Name.Length <= 1) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             foreach (var option in invalidNameLengthOptions) |  | ||||||
|             { |  | ||||||
|                 context.ReportDiagnostic( |  | ||||||
|                     Diagnostic.Create(DiagnosticDescriptors.CliFx0042, option.Property.Locations.First())); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // Duplicate name |  | ||||||
|             var duplicateNameOptions = options |  | ||||||
|                 .Where(p => !string.IsNullOrWhiteSpace(p.Name)) |  | ||||||
|                 .GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase) |  | ||||||
|                 .Where(g => g.Count() > 1) |  | ||||||
|                 .SelectMany(g => g.AsEnumerable()) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             foreach (var option in duplicateNameOptions) |  | ||||||
|             { |  | ||||||
|                 context.ReportDiagnostic( |  | ||||||
|                     Diagnostic.Create(DiagnosticDescriptors.CliFx0043, option.Property.Locations.First())); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // Duplicate name |  | ||||||
|             var duplicateShortNameOptions = options |  | ||||||
|                 .Where(p => p.ShortName != null) |  | ||||||
|                 .GroupBy(p => p.ShortName) |  | ||||||
|                 .Where(g => g.Count() > 1) |  | ||||||
|                 .SelectMany(g => g.AsEnumerable()) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             foreach (var option in duplicateShortNameOptions) |  | ||||||
|             { |  | ||||||
|                 context.ReportDiagnostic( |  | ||||||
|                     Diagnostic.Create(DiagnosticDescriptors.CliFx0044, option.Property.Locations.First())); |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             // Duplicate environment variable name |  | ||||||
|             var duplicateEnvironmentVariableNameOptions = options |  | ||||||
|                 .Where(p => !string.IsNullOrWhiteSpace(p.EnvironmentVariableName)) |  | ||||||
|                 .GroupBy(p => p.EnvironmentVariableName, StringComparer.OrdinalIgnoreCase) |  | ||||||
|                 .Where(g => g.Count() > 1) |  | ||||||
|                 .SelectMany(g => g.AsEnumerable()) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             foreach (var option in duplicateEnvironmentVariableNameOptions) |  | ||||||
|             { |  | ||||||
|                 context.ReportDiagnostic( |  | ||||||
|                     Diagnostic.Create(DiagnosticDescriptors.CliFx0045, option.Property.Locations.First())); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private static void CheckCommandType(SymbolAnalysisContext context) |  | ||||||
|         { |  | ||||||
|             // Named type: MyCommand |  | ||||||
|             if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol)) |  | ||||||
|                 return; |  | ||||||
|  |  | ||||||
|             // Only classes |  | ||||||
|             if (namedTypeSymbol.TypeKind != TypeKind.Class) |  | ||||||
|                 return; |  | ||||||
|  |  | ||||||
|             // Implements ICommand? |  | ||||||
|             var implementsCommandInterface = namedTypeSymbol |  | ||||||
|                 .AllInterfaces |  | ||||||
|                 .Any(KnownSymbols.IsCommandInterface); |  | ||||||
|  |  | ||||||
|             // Has CommandAttribute? |  | ||||||
|             var hasCommandAttribute = namedTypeSymbol |  | ||||||
|                 .GetAttributes() |  | ||||||
|                 .Select(a => a.AttributeClass) |  | ||||||
|                 .Any(KnownSymbols.IsCommandAttribute); |  | ||||||
|  |  | ||||||
|             var isValidCommandType = |  | ||||||
|                 // implements interface |  | ||||||
|                 implementsCommandInterface && ( |  | ||||||
|                     // and either abstract class or has attribute |  | ||||||
|                     namedTypeSymbol.IsAbstract || hasCommandAttribute |  | ||||||
|                 ); |  | ||||||
|  |  | ||||||
|             if (!isValidCommandType) |  | ||||||
|             { |  | ||||||
|                 // See if this was meant to be a command type (either interface or attribute present) |  | ||||||
|                 var isAlmostValidCommandType = implementsCommandInterface ^ hasCommandAttribute; |  | ||||||
|  |  | ||||||
|                 if (isAlmostValidCommandType && !implementsCommandInterface) |  | ||||||
|                     context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0001, namedTypeSymbol.Locations.First())); |  | ||||||
|  |  | ||||||
|                 if (isAlmostValidCommandType && !hasCommandAttribute) |  | ||||||
|                     context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0002, namedTypeSymbol.Locations.First())); |  | ||||||
|  |  | ||||||
|                 return; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             var properties = namedTypeSymbol |  | ||||||
|                 .GetMembers() |  | ||||||
|                 .Where(m => m.Kind == SymbolKind.Property) |  | ||||||
|                 .OfType<IPropertySymbol>().ToArray(); |  | ||||||
|  |  | ||||||
|             // Check parameters |  | ||||||
|             var parameterProperties = properties |  | ||||||
|                 .Where(p => p |  | ||||||
|                     .GetAttributes() |  | ||||||
|                     .Select(a => a.AttributeClass) |  | ||||||
|                     .Any(KnownSymbols.IsCommandParameterAttribute)) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             CheckCommandParameterProperties(context, parameterProperties); |  | ||||||
|  |  | ||||||
|             // Check options |  | ||||||
|             var optionsProperties = properties |  | ||||||
|                 .Where(p => p |  | ||||||
|                     .GetAttributes() |  | ||||||
|                     .Select(a => a.AttributeClass) |  | ||||||
|                     .Any(KnownSymbols.IsCommandOptionAttribute)) |  | ||||||
|                 .ToArray(); |  | ||||||
|  |  | ||||||
|             CheckCommandOptionProperties(context, optionsProperties); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public override void Initialize(AnalysisContext context) |  | ||||||
|         { |  | ||||||
|             context.EnableConcurrentExecution(); |  | ||||||
|             context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |  | ||||||
|  |  | ||||||
|             context.RegisterSymbolAction(CheckCommandType, SymbolKind.NamedType); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,80 +0,0 @@ | |||||||
| using System.Collections.Immutable; |  | ||||||
| using System.Linq; |  | ||||||
| using Microsoft.CodeAnalysis; |  | ||||||
| using Microsoft.CodeAnalysis.CSharp; |  | ||||||
| using Microsoft.CodeAnalysis.CSharp.Syntax; |  | ||||||
| using Microsoft.CodeAnalysis.Diagnostics; |  | ||||||
|  |  | ||||||
| namespace CliFx.Analyzers |  | ||||||
| { |  | ||||||
|     [DiagnosticAnalyzer(LanguageNames.CSharp)] |  | ||||||
|     public class ConsoleUsageAnalyzer : DiagnosticAnalyzer |  | ||||||
|     { |  | ||||||
|         public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create( |  | ||||||
|             DiagnosticDescriptors.CliFx0100 |  | ||||||
|         ); |  | ||||||
|  |  | ||||||
|         private static bool IsSystemConsoleInvocation( |  | ||||||
|             SyntaxNodeAnalysisContext context, |  | ||||||
|             InvocationExpressionSyntax invocationSyntax) |  | ||||||
|         { |  | ||||||
|             // Get the method member access (Console.WriteLine or Console.Error.WriteLine) |  | ||||||
|             if (!(invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax)) |  | ||||||
|                 return false; |  | ||||||
|  |  | ||||||
|             // Get the semantic model for the invoked method |  | ||||||
|             if (!(context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol)) |  | ||||||
|                 return false; |  | ||||||
|  |  | ||||||
|             // Check if contained within System.Console |  | ||||||
|             if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType)) |  | ||||||
|                 return true; |  | ||||||
|  |  | ||||||
|             // In case with Console.Error.WriteLine that wouldn't work, we need to check parent member access too |  | ||||||
|             if (!(memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax)) |  | ||||||
|                 return false; |  | ||||||
|  |  | ||||||
|             // Get the semantic model for the parent member |  | ||||||
|             if (!(context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol)) |  | ||||||
|                 return false; |  | ||||||
|  |  | ||||||
|             // Check if contained within System.Console |  | ||||||
|             if (KnownSymbols.IsSystemConsole(propertySymbol.ContainingType)) |  | ||||||
|                 return true; |  | ||||||
|  |  | ||||||
|             return false; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private static void CheckSystemConsoleUsage(SyntaxNodeAnalysisContext context) |  | ||||||
|         { |  | ||||||
|             if (!(context.Node is InvocationExpressionSyntax invocationSyntax)) |  | ||||||
|                 return; |  | ||||||
|  |  | ||||||
|             if (!IsSystemConsoleInvocation(context, invocationSyntax)) |  | ||||||
|                 return; |  | ||||||
|  |  | ||||||
|             // Check if IConsole is available in the scope as a viable alternative |  | ||||||
|             var isConsoleInterfaceAvailable = invocationSyntax |  | ||||||
|                 .Ancestors() |  | ||||||
|                 .OfType<MethodDeclarationSyntax>() |  | ||||||
|                 .SelectMany(m => m.ParameterList.Parameters) |  | ||||||
|                 .Select(p => p.Type) |  | ||||||
|                 .Select(t => context.SemanticModel.GetSymbolInfo(t).Symbol) |  | ||||||
|                 .Where(s => s != null) |  | ||||||
|                 .Any(KnownSymbols.IsConsoleInterface!); |  | ||||||
|  |  | ||||||
|             if (!isConsoleInterfaceAvailable) |  | ||||||
|                 return; |  | ||||||
|  |  | ||||||
|             context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0100, invocationSyntax.GetLocation())); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public override void Initialize(AnalysisContext context) |  | ||||||
|         { |  | ||||||
|             context.EnableConcurrentExecution(); |  | ||||||
|             context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |  | ||||||
|  |  | ||||||
|             context.RegisterSyntaxNodeAction(CheckSystemConsoleUsage, SyntaxKind.InvocationExpression); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,79 +0,0 @@ | |||||||
| using Microsoft.CodeAnalysis; |  | ||||||
|  |  | ||||||
| namespace CliFx.Analyzers |  | ||||||
| { |  | ||||||
|     public static class DiagnosticDescriptors |  | ||||||
|     { |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0001 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0001), |  | ||||||
|                 "Type must implement the 'CliFx.ICommand' interface in order to be a valid command", |  | ||||||
|                 "Type must implement the 'CliFx.ICommand' interface in order to be a valid command", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0002 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0002), |  | ||||||
|                 "Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command", |  | ||||||
|                 "Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0021 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0021), |  | ||||||
|                 "Parameter order must be unique within its command", |  | ||||||
|                 "Parameter order must be unique within its command", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0022 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0022), |  | ||||||
|                 "Parameter order must have unique name within its command", |  | ||||||
|                 "Parameter order must have unique name within its command", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0023 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0023), |  | ||||||
|                 "Only one non-scalar parameter per command is allowed", |  | ||||||
|                 "Only one non-scalar parameter per command is allowed", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0024 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0024), |  | ||||||
|                 "Non-scalar parameter must be last in order", |  | ||||||
|                 "Non-scalar parameter must be last in order", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0041 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0041), |  | ||||||
|                 "Option must have a name or short name specified", |  | ||||||
|                 "Option must have a name or short name specified", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0042 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0042), |  | ||||||
|                 "Option name must be at least 2 characters long", |  | ||||||
|                 "Option name must be at least 2 characters long", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0043 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0043), |  | ||||||
|                 "Option name must be unique within its command", |  | ||||||
|                 "Option name must be unique within its command", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0044 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0044), |  | ||||||
|                 "Option short name must be unique within its command", |  | ||||||
|                 "Option short name must be unique within its command", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0045 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0045), |  | ||||||
|                 "Option environment variable name must be unique within its command", |  | ||||||
|                 "Option environment variable name must be unique within its command", |  | ||||||
|                 "Usage", DiagnosticSeverity.Error, true); |  | ||||||
|  |  | ||||||
|         public static readonly DiagnosticDescriptor CliFx0100 = |  | ||||||
|             new DiagnosticDescriptor(nameof(CliFx0100), |  | ||||||
|                 "Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation", |  | ||||||
|                 "Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation", |  | ||||||
|                 "Usage", DiagnosticSeverity.Warning, true); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,11 +0,0 @@ | |||||||
| using System; |  | ||||||
| using Microsoft.CodeAnalysis; |  | ||||||
|  |  | ||||||
| namespace CliFx.Analyzers.Internal |  | ||||||
| { |  | ||||||
|     internal static class RoslynExtensions |  | ||||||
|     { |  | ||||||
|         public static bool DisplayNameMatches(this ISymbol symbol, string name) => |  | ||||||
|             string.Equals(symbol.ToDisplayString(), name, StringComparison.Ordinal); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,37 +0,0 @@ | |||||||
| using CliFx.Analyzers.Internal; |  | ||||||
| using Microsoft.CodeAnalysis; |  | ||||||
|  |  | ||||||
| namespace CliFx.Analyzers |  | ||||||
| { |  | ||||||
|     public static class KnownSymbols |  | ||||||
|     { |  | ||||||
|         public static bool IsSystemString(ISymbol symbol) => |  | ||||||
|             symbol.DisplayNameMatches("string") || |  | ||||||
|             symbol.DisplayNameMatches("System.String"); |  | ||||||
|  |  | ||||||
|         public static bool IsSystemChar(ISymbol symbol) => |  | ||||||
|             symbol.DisplayNameMatches("char") || |  | ||||||
|             symbol.DisplayNameMatches("System.Char"); |  | ||||||
|  |  | ||||||
|         public static bool IsSystemCollectionsGenericIEnumerable(ISymbol symbol) => |  | ||||||
|             symbol.DisplayNameMatches("System.Collections.Generic.IEnumerable<T>"); |  | ||||||
|  |  | ||||||
|         public static bool IsSystemConsole(ISymbol symbol) => |  | ||||||
|             symbol.DisplayNameMatches("System.Console"); |  | ||||||
|  |  | ||||||
|         public static bool IsConsoleInterface(ISymbol symbol) => |  | ||||||
|             symbol.DisplayNameMatches("CliFx.IConsole"); |  | ||||||
|  |  | ||||||
|         public static bool IsCommandInterface(ISymbol symbol) => |  | ||||||
|             symbol.DisplayNameMatches("CliFx.ICommand"); |  | ||||||
|  |  | ||||||
|         public static bool IsCommandAttribute(ISymbol symbol) => |  | ||||||
|             symbol.DisplayNameMatches("CliFx.Attributes.CommandAttribute"); |  | ||||||
|  |  | ||||||
|         public static bool IsCommandParameterAttribute(ISymbol symbol) => |  | ||||||
|             symbol.DisplayNameMatches("CliFx.Attributes.CommandParameterAttribute"); |  | ||||||
|  |  | ||||||
|         public static bool IsCommandOptionAttribute(ISymbol symbol) => |  | ||||||
|             symbol.DisplayNameMatches("CliFx.Attributes.CommandOptionAttribute"); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
							
								
								
									
										32
									
								
								CliFx.Benchmarks/Benchmarks.CliFx.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								CliFx.Benchmarks/Benchmarks.CliFx.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,32 @@ | |||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Threading.Tasks; | ||||||
|  | using BenchmarkDotNet.Attributes; | ||||||
|  | using CliFx.Attributes; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  |  | ||||||
|  | namespace CliFx.Benchmarks; | ||||||
|  |  | ||||||
|  | public partial class Benchmarks | ||||||
|  | { | ||||||
|  |     [Command] | ||||||
|  |     public class CliFxCommand : ICommand | ||||||
|  |     { | ||||||
|  |         [CommandOption("str", 's')] | ||||||
|  |         public string? StrOption { get; set; } | ||||||
|  |  | ||||||
|  |         [CommandOption("int", 'i')] | ||||||
|  |         public int IntOption { get; set; } | ||||||
|  |  | ||||||
|  |         [CommandOption("bool", 'b')] | ||||||
|  |         public bool BoolOption { get; set; } | ||||||
|  |  | ||||||
|  |         public ValueTask ExecuteAsync(IConsole console) => default; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Benchmark(Description = "CliFx", Baseline = true)] | ||||||
|  |     public async ValueTask<int> ExecuteWithCliFx() => | ||||||
|  |         await new CliApplicationBuilder() | ||||||
|  |             .AddCommand<CliFxCommand>() | ||||||
|  |             .Build() | ||||||
|  |             .RunAsync(Arguments, new Dictionary<string, string>()); | ||||||
|  | } | ||||||
| @@ -1,6 +1,9 @@ | |||||||
| using clipr; | using BenchmarkDotNet.Attributes; | ||||||
|  | using clipr; | ||||||
| 
 | 
 | ||||||
| namespace CliFx.Benchmarks.Commands | namespace CliFx.Benchmarks; | ||||||
|  | 
 | ||||||
|  | public partial class Benchmarks | ||||||
| { | { | ||||||
|     public class CliprCommand |     public class CliprCommand | ||||||
|     { |     { | ||||||
| @@ -13,8 +16,9 @@ namespace CliFx.Benchmarks.Commands | |||||||
|         [NamedArgument('b', "bool", Constraint = NumArgsConstraint.Optional, Const = true)] |         [NamedArgument('b', "bool", Constraint = NumArgsConstraint.Optional, Const = true)] | ||||||
|         public bool BoolOption { get; set; } |         public bool BoolOption { get; set; } | ||||||
| 
 | 
 | ||||||
|         public void Execute() |         public void Execute() { } | ||||||
|         { |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     [Benchmark(Description = "Clipr")] | ||||||
|  |     public void ExecuteWithClipr() => CliParser.Parse<CliprCommand>(Arguments).Execute(); | ||||||
| } | } | ||||||
							
								
								
									
										19
									
								
								CliFx.Benchmarks/Benchmarks.Cocona.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								CliFx.Benchmarks/Benchmarks.Cocona.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | using BenchmarkDotNet.Attributes; | ||||||
|  | using Cocona; | ||||||
|  |  | ||||||
|  | namespace CliFx.Benchmarks; | ||||||
|  |  | ||||||
|  | public partial class Benchmarks | ||||||
|  | { | ||||||
|  |     public class CoconaCommand | ||||||
|  |     { | ||||||
|  |         public void Execute( | ||||||
|  |             [Option("str", ['s'])] string? strOption, | ||||||
|  |             [Option("int", ['i'])] int intOption, | ||||||
|  |             [Option("bool", ['b'])] bool boolOption | ||||||
|  |         ) { } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Benchmark(Description = "Cocona")] | ||||||
|  |     public void ExecuteWithCocona() => CoconaApp.Run<CoconaCommand>(Arguments); | ||||||
|  | } | ||||||
							
								
								
									
										27
									
								
								CliFx.Benchmarks/Benchmarks.CommandLineParser.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								CliFx.Benchmarks/Benchmarks.CommandLineParser.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | using BenchmarkDotNet.Attributes; | ||||||
|  | using CommandLine; | ||||||
|  |  | ||||||
|  | namespace CliFx.Benchmarks; | ||||||
|  |  | ||||||
|  | public partial class Benchmarks | ||||||
|  | { | ||||||
|  |     public class CommandLineParserCommand | ||||||
|  |     { | ||||||
|  |         [Option('s', "str")] | ||||||
|  |         public string? StrOption { get; set; } | ||||||
|  |  | ||||||
|  |         [Option('i', "int")] | ||||||
|  |         public int IntOption { get; set; } | ||||||
|  |  | ||||||
|  |         [Option('b', "bool")] | ||||||
|  |         public bool BoolOption { get; set; } | ||||||
|  |  | ||||||
|  |         public void Execute() { } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Benchmark(Description = "CommandLineParser")] | ||||||
|  |     public void ExecuteWithCommandLineParser() => | ||||||
|  |         new Parser() | ||||||
|  |             .ParseArguments(Arguments, typeof(CommandLineParserCommand)) | ||||||
|  |             .WithParsed<CommandLineParserCommand>(c => c.Execute()); | ||||||
|  | } | ||||||
							
								
								
									
										24
									
								
								CliFx.Benchmarks/Benchmarks.McMaster.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								CliFx.Benchmarks/Benchmarks.McMaster.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,24 @@ | |||||||
|  | using BenchmarkDotNet.Attributes; | ||||||
|  | using McMaster.Extensions.CommandLineUtils; | ||||||
|  |  | ||||||
|  | namespace CliFx.Benchmarks; | ||||||
|  |  | ||||||
|  | public partial class Benchmarks | ||||||
|  | { | ||||||
|  |     public class McMasterCommand | ||||||
|  |     { | ||||||
|  |         [Option("--str|-s")] | ||||||
|  |         public string? StrOption { get; set; } | ||||||
|  |  | ||||||
|  |         [Option("--int|-i")] | ||||||
|  |         public int IntOption { get; set; } | ||||||
|  |  | ||||||
|  |         [Option("--bool|-b")] | ||||||
|  |         public bool BoolOption { get; set; } | ||||||
|  |  | ||||||
|  |         public int OnExecute() => 0; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Benchmark(Description = "McMaster.Extensions.CommandLineUtils")] | ||||||
|  |     public int ExecuteWithMcMaster() => CommandLineApplication.Execute<McMasterCommand>(Arguments); | ||||||
|  | } | ||||||
| @@ -1,6 +1,9 @@ | |||||||
| using PowerArgs; | using BenchmarkDotNet.Attributes; | ||||||
|  | using PowerArgs; | ||||||
| 
 | 
 | ||||||
| namespace CliFx.Benchmarks.Commands | namespace CliFx.Benchmarks; | ||||||
|  | 
 | ||||||
|  | public partial class Benchmarks | ||||||
| { | { | ||||||
|     public class PowerArgsCommand |     public class PowerArgsCommand | ||||||
|     { |     { | ||||||
| @@ -13,8 +16,9 @@ namespace CliFx.Benchmarks.Commands | |||||||
|         [ArgShortcut("--bool"), ArgShortcut("-b")] |         [ArgShortcut("--bool"), ArgShortcut("-b")] | ||||||
|         public bool BoolOption { get; set; } |         public bool BoolOption { get; set; } | ||||||
| 
 | 
 | ||||||
|         public void Main() |         public void Main() { } | ||||||
|         { |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     [Benchmark(Description = "PowerArgs")] | ||||||
|  |     public void ExecuteWithPowerArgs() => Args.InvokeMain<PowerArgsCommand>(Arguments); | ||||||
| } | } | ||||||
							
								
								
									
										33
									
								
								CliFx.Benchmarks/Benchmarks.SystemCommandLine.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								CliFx.Benchmarks/Benchmarks.SystemCommandLine.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | |||||||
|  | using System.CommandLine; | ||||||
|  | using System.Threading.Tasks; | ||||||
|  | using BenchmarkDotNet.Attributes; | ||||||
|  |  | ||||||
|  | namespace CliFx.Benchmarks; | ||||||
|  |  | ||||||
|  | public partial class Benchmarks | ||||||
|  | { | ||||||
|  |     public class SystemCommandLineCommand | ||||||
|  |     { | ||||||
|  |         public static void ExecuteHandler(string s, int i, bool b) { } | ||||||
|  |  | ||||||
|  |         public Task<int> ExecuteAsync(string[] args) | ||||||
|  |         { | ||||||
|  |             var stringOption = new Option<string>(["--str", "-s"]); | ||||||
|  |             var intOption = new Option<int>(["--int", "-i"]); | ||||||
|  |             var boolOption = new Option<bool>(["--bool", "-b"]); | ||||||
|  |  | ||||||
|  |             var command = new RootCommand(); | ||||||
|  |             command.AddOption(stringOption); | ||||||
|  |             command.AddOption(intOption); | ||||||
|  |             command.AddOption(boolOption); | ||||||
|  |  | ||||||
|  |             command.SetHandler(ExecuteHandler, stringOption, intOption, boolOption); | ||||||
|  |  | ||||||
|  |             return command.InvokeAsync(args); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Benchmark(Description = "System.CommandLine")] | ||||||
|  |     public async Task<int> ExecuteWithSystemCommandLine() => | ||||||
|  |         await new SystemCommandLineCommand().ExecuteAsync(Arguments); | ||||||
|  | } | ||||||
| @@ -1,52 +1,18 @@ | |||||||
| using System.Collections.Generic; | using BenchmarkDotNet.Attributes; | ||||||
| using System.Threading.Tasks; |  | ||||||
| using BenchmarkDotNet.Attributes; |  | ||||||
| using BenchmarkDotNet.Configs; | using BenchmarkDotNet.Configs; | ||||||
| using BenchmarkDotNet.Order; | using BenchmarkDotNet.Order; | ||||||
| using BenchmarkDotNet.Running; | using BenchmarkDotNet.Running; | ||||||
| using CliFx.Benchmarks.Commands; |  | ||||||
| using CommandLine; |  | ||||||
|  |  | ||||||
| namespace CliFx.Benchmarks | namespace CliFx.Benchmarks; | ||||||
|  |  | ||||||
|  | [RankColumn] | ||||||
|  | [Orderer(SummaryOrderPolicy.FastestToSlowest)] | ||||||
|  | public partial class Benchmarks | ||||||
| { | { | ||||||
|     [SimpleJob] |     private static readonly string[] Arguments = ["--str", "hello world", "-i", "13", "-b"]; | ||||||
|     [RankColumn] |  | ||||||
|     [Orderer(SummaryOrderPolicy.FastestToSlowest)] |  | ||||||
|     public class Benchmarks |  | ||||||
|     { |  | ||||||
|         private static readonly string[] Arguments = {"--str", "hello world", "-i", "13", "-b"}; |  | ||||||
|  |  | ||||||
|         [Benchmark(Description = "CliFx", Baseline = true)] |  | ||||||
|         public async ValueTask<int> ExecuteWithCliFx() => |  | ||||||
|             await new CliApplicationBuilder().AddCommand(typeof(CliFxCommand)).Build().RunAsync(Arguments, new Dictionary<string, string>()); |  | ||||||
|  |  | ||||||
|         [Benchmark(Description = "System.CommandLine")] |  | ||||||
|         public async Task<int> ExecuteWithSystemCommandLine() => |  | ||||||
|             await new SystemCommandLineCommand().ExecuteAsync(Arguments); |  | ||||||
|  |  | ||||||
|         [Benchmark(Description = "McMaster.Extensions.CommandLineUtils")] |  | ||||||
|         public int ExecuteWithMcMaster() => |  | ||||||
|             McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute<McMasterCommand>(Arguments); |  | ||||||
|  |  | ||||||
|         [Benchmark(Description = "CommandLineParser")] |  | ||||||
|         public void ExecuteWithCommandLineParser() => |  | ||||||
|             new Parser() |  | ||||||
|                 .ParseArguments(Arguments, typeof(CommandLineParserCommand)) |  | ||||||
|                 .WithParsed<CommandLineParserCommand>(c => c.Execute()); |  | ||||||
|  |  | ||||||
|         [Benchmark(Description = "PowerArgs")] |  | ||||||
|         public void ExecuteWithPowerArgs() => |  | ||||||
|             PowerArgs.Args.InvokeMain<PowerArgsCommand>(Arguments); |  | ||||||
|  |  | ||||||
|         [Benchmark(Description = "Clipr")] |  | ||||||
|         public void ExecuteWithClipr() => |  | ||||||
|             clipr.CliParser.Parse<CliprCommand>(Arguments).Execute(); |  | ||||||
|  |  | ||||||
|         [Benchmark(Description = "Cocona")] |  | ||||||
|         public void ExecuteWithCocona() => |  | ||||||
|             Cocona.CoconaApp.Run<CoconaCommand>(Arguments); |  | ||||||
|  |  | ||||||
|     public static void Main() => |     public static void Main() => | ||||||
|             BenchmarkRunner.Run<Benchmarks>(DefaultConfig.Instance.With(ConfigOptions.DisableOptimizationsValidator)); |         BenchmarkRunner.Run<Benchmarks>( | ||||||
|     } |             DefaultConfig.Instance.WithOptions(ConfigOptions.DisableOptimizationsValidator) | ||||||
|  |         ); | ||||||
| } | } | ||||||
| @@ -1,19 +1,20 @@ | |||||||
| <Project Sdk="Microsoft.NET.Sdk"> | <Project Sdk="Microsoft.NET.Sdk"> | ||||||
|   <Import Project="../CliFx.props" /> |  | ||||||
|  |  | ||||||
|   <PropertyGroup> |   <PropertyGroup> | ||||||
|     <OutputType>Exe</OutputType> |     <OutputType>Exe</OutputType> | ||||||
|     <TargetFramework>netcoreapp3.1</TargetFramework> |     <TargetFramework>net9.0</TargetFramework> | ||||||
|  |     <NuGetAudit>false</NuGetAudit> | ||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <PackageReference Include="BenchmarkDotNet" Version="0.12.0" /> |     <PackageReference Include="BenchmarkDotNet" Version="0.15.2" /> | ||||||
|     <PackageReference Include="clipr" Version="1.6.1" /> |     <PackageReference Include="clipr" Version="1.6.1" /> | ||||||
|     <PackageReference Include="Cocona" Version="1.3.0" /> |     <PackageReference Include="Cocona" Version="2.2.0" /> | ||||||
|     <PackageReference Include="CommandLineParser" Version="2.7.82" /> |     <PackageReference Include="CommandLineParser" Version="2.9.1" /> | ||||||
|     <PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.6.0" /> |     <PackageReference Include="CSharpier.MsBuild" Version="1.1.2" PrivateAssets="all" /> | ||||||
|     <PackageReference Include="PowerArgs" Version="3.6.0" /> |     <PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" /> | ||||||
|     <PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" /> |     <PackageReference Include="PowerArgs" Version="4.0.3" /> | ||||||
|  |     <PackageReference Include="System.CommandLine" Version="2.0.0-beta6.25358.103" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|   | |||||||
| @@ -1,20 +0,0 @@ | |||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
|  |  | ||||||
| namespace CliFx.Benchmarks.Commands |  | ||||||
| { |  | ||||||
|     [Command] |  | ||||||
|     public class CliFxCommand : ICommand |  | ||||||
|     { |  | ||||||
|         [CommandOption("str", 's')] |  | ||||||
|         public string? StrOption { get; set; } |  | ||||||
|  |  | ||||||
|         [CommandOption("int", 'i')] |  | ||||||
|         public int IntOption { get; set; } |  | ||||||
|  |  | ||||||
|         [CommandOption("bool", 'b')] |  | ||||||
|         public bool BoolOption { get; set; } |  | ||||||
|  |  | ||||||
|         public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,17 +0,0 @@ | |||||||
| using Cocona; |  | ||||||
|  |  | ||||||
| namespace CliFx.Benchmarks.Commands |  | ||||||
| { |  | ||||||
|     public class CoconaCommand |  | ||||||
|     { |  | ||||||
|         public void Execute( |  | ||||||
|             [Option("str", new []{'s'})] |  | ||||||
|             string? strOption, |  | ||||||
|             [Option("int", new []{'i'})] |  | ||||||
|             int intOption, |  | ||||||
|             [Option("bool", new []{'b'})] |  | ||||||
|             bool boolOption) |  | ||||||
|         { |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,20 +0,0 @@ | |||||||
| using CommandLine; |  | ||||||
|  |  | ||||||
| namespace CliFx.Benchmarks.Commands |  | ||||||
| { |  | ||||||
|     public class CommandLineParserCommand |  | ||||||
|     { |  | ||||||
|         [Option('s', "str")] |  | ||||||
|         public string? StrOption { get; set; } |  | ||||||
|  |  | ||||||
|         [Option('i', "int")] |  | ||||||
|         public int IntOption { get; set; } |  | ||||||
|  |  | ||||||
|         [Option('b', "bool")] |  | ||||||
|         public bool BoolOption { get; set; } |  | ||||||
|  |  | ||||||
|         public void Execute() |  | ||||||
|         { |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,18 +0,0 @@ | |||||||
| using McMaster.Extensions.CommandLineUtils; |  | ||||||
|  |  | ||||||
| namespace CliFx.Benchmarks.Commands |  | ||||||
| { |  | ||||||
|     public class McMasterCommand |  | ||||||
|     { |  | ||||||
|         [Option("--str|-s")] |  | ||||||
|         public string? StrOption { get; set; } |  | ||||||
|  |  | ||||||
|         [Option("--int|-i")] |  | ||||||
|         public int IntOption { get; set; } |  | ||||||
|  |  | ||||||
|         [Option("--bool|-b")] |  | ||||||
|         public bool BoolOption { get; set; } |  | ||||||
|  |  | ||||||
|         public int OnExecute() => 0; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,34 +0,0 @@ | |||||||
| using System.CommandLine; |  | ||||||
| using System.CommandLine.Invocation; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
|  |  | ||||||
| namespace CliFx.Benchmarks.Commands |  | ||||||
| { |  | ||||||
|     public class SystemCommandLineCommand |  | ||||||
|     { |  | ||||||
|         public static int ExecuteHandler(string s, int i, bool b) => 0; |  | ||||||
|  |  | ||||||
|         public Task<int> ExecuteAsync(string[] args) |  | ||||||
|         { |  | ||||||
|             var command = new RootCommand |  | ||||||
|             { |  | ||||||
|                 new Option(new[] {"--str", "-s"}) |  | ||||||
|                 { |  | ||||||
|                     Argument = new Argument<string?>() |  | ||||||
|                 }, |  | ||||||
|                 new Option(new[] {"--int", "-i"}) |  | ||||||
|                 { |  | ||||||
|                     Argument = new Argument<int>() |  | ||||||
|                 }, |  | ||||||
|                 new Option(new[] {"--bool", "-b"}) |  | ||||||
|                 { |  | ||||||
|                     Argument = new Argument<bool>() |  | ||||||
|                 } |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             command.Handler = CommandHandler.Create(typeof(SystemCommandLineCommand).GetMethod(nameof(ExecuteHandler))); |  | ||||||
|  |  | ||||||
|             return command.InvokeAsync(args); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
							
								
								
									
										22
									
								
								CliFx.Benchmarks/Readme.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								CliFx.Benchmarks/Readme.md
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | |||||||
|  | ## CliFx.Benchmarks | ||||||
|  |  | ||||||
|  | All benchmarks below were ran with the following configuration: | ||||||
|  |  | ||||||
|  | ```ini | ||||||
|  | BenchmarkDotNet=v0.12.0, OS=Windows 10.0.14393.3443 (1607/AnniversaryUpdate/Redstone1) | ||||||
|  | Intel Core i5-4460 CPU 3.20GHz (Haswell), 1 CPU, 4 logical and 4 physical cores | ||||||
|  | Frequency=3124994 Hz, Resolution=320.0006 ns, Timer=TSC | ||||||
|  | .NET Core SDK=3.1.100 | ||||||
|  |   [Host]     : .NET Core 3.1.0 (CoreCLR 4.700.19.56402, CoreFX 4.700.19.56404), X64 RyuJIT | ||||||
|  |   DefaultJob : .NET Core 3.1.0 (CoreCLR 4.700.19.56402, CoreFX 4.700.19.56404), X64 RyuJIT | ||||||
|  | ``` | ||||||
|  |  | ||||||
|  | | Method                               |        Mean |     Error |     StdDev | Ratio | RatioSD | Rank | | ||||||
|  | | ------------------------------------ | ----------: | --------: | ---------: | ----: | ------: | ---: | | ||||||
|  | | CommandLineParser                    |    24.79 us |  0.166 us |   0.155 us |  0.49 |    0.00 |    1 | | ||||||
|  | | CliFx                                |    50.27 us |  0.248 us |   0.232 us |  1.00 |    0.00 |    2 | | ||||||
|  | | Clipr                                |   160.22 us |  0.817 us |   0.764 us |  3.19 |    0.02 |    3 | | ||||||
|  | | McMaster.Extensions.CommandLineUtils |   166.45 us |  1.111 us |   1.039 us |  3.31 |    0.03 |    4 | | ||||||
|  | | System.CommandLine                   |   170.27 us |  0.599 us |   0.560 us |  3.39 |    0.02 |    5 | | ||||||
|  | | PowerArgs                            |   306.12 us |  1.495 us |   1.398 us |  6.09 |    0.03 |    6 | | ||||||
|  | | Cocona                               | 1,856.07 us | 48.727 us | 141.367 us | 37.88 |    2.60 |    7 | | ||||||
| @@ -1,19 +1,20 @@ | |||||||
| <Project Sdk="Microsoft.NET.Sdk"> | <Project Sdk="Microsoft.NET.Sdk"> | ||||||
|   <Import Project="../CliFx.props" /> |  | ||||||
|  |  | ||||||
|   <PropertyGroup> |   <PropertyGroup> | ||||||
|     <OutputType>Exe</OutputType> |     <OutputType>Exe</OutputType> | ||||||
|     <TargetFramework>netcoreapp3.1</TargetFramework> |     <TargetFramework>net9.0</TargetFramework> | ||||||
|  |     <ApplicationIcon>../favicon.ico</ApplicationIcon> | ||||||
|  |     <PublishAot>true</PublishAot> | ||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" /> |     <PackageReference Include="CSharpier.MsBuild" Version="1.1.2" PrivateAssets="all" /> | ||||||
|     <PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> |     <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.9" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <ProjectReference Include="..\CliFx\CliFx.csproj" /> |     <ProjectReference Include="..\CliFx\CliFx.csproj" /> | ||||||
|     <ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" /> |     <ProjectReference Include="..\CliFx.SourceGeneration\CliFx.SourceGeneration.csproj" ReferenceOutputAssembly="false" OutputItemType="analyzer" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|  |  | ||||||
| </Project> | </Project> | ||||||
| @@ -1,68 +1,55 @@ | |||||||
| using System; | using System; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| using CliFx.Attributes; | using CliFx.Attributes; | ||||||
| using CliFx.Demo.Internal; | using CliFx.Demo.Domain; | ||||||
| using CliFx.Demo.Models; | using CliFx.Demo.Utils; | ||||||
| using CliFx.Demo.Services; |  | ||||||
| using CliFx.Exceptions; | using CliFx.Exceptions; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Commands | namespace CliFx.Demo.Commands; | ||||||
|  |  | ||||||
|  | [Command("book add", Description = "Adds a book to the library.")] | ||||||
|  | public class BookAddCommand(LibraryProvider libraryProvider) : ICommand | ||||||
| { | { | ||||||
|     [Command("book add", Description = "Add a book to the library.")] |  | ||||||
|     public partial class BookAddCommand : ICommand |  | ||||||
|     { |  | ||||||
|         private readonly LibraryService _libraryService; |  | ||||||
|  |  | ||||||
|     [CommandParameter(0, Name = "title", Description = "Book title.")] |     [CommandParameter(0, Name = "title", Description = "Book title.")] | ||||||
|         public string Title { get; set; } = ""; |     public required string Title { get; init; } | ||||||
|  |  | ||||||
|         [CommandOption("author", 'a', IsRequired = true, Description = "Book author.")] |     [CommandOption("author", 'a', Description = "Book author.")] | ||||||
|         public string Author { get; set; } = ""; |     public required string Author { get; init; } | ||||||
|  |  | ||||||
|     [CommandOption("published", 'p', Description = "Book publish date.")] |     [CommandOption("published", 'p', Description = "Book publish date.")] | ||||||
|         public DateTimeOffset Published { get; set; } = CreateRandomDate(); |     public DateTimeOffset Published { get; init; } = | ||||||
|  |         new( | ||||||
|  |             Random.Shared.Next(1800, 2020), | ||||||
|  |             Random.Shared.Next(1, 12), | ||||||
|  |             Random.Shared.Next(1, 28), | ||||||
|  |             Random.Shared.Next(1, 23), | ||||||
|  |             Random.Shared.Next(1, 59), | ||||||
|  |             Random.Shared.Next(1, 59), | ||||||
|  |             TimeSpan.Zero | ||||||
|  |         ); | ||||||
|  |  | ||||||
|     [CommandOption("isbn", 'n', Description = "Book ISBN.")] |     [CommandOption("isbn", 'n', Description = "Book ISBN.")] | ||||||
|         public Isbn Isbn { get; set; } = CreateRandomIsbn(); |     public Isbn Isbn { get; init; } = | ||||||
|  |         new( | ||||||
|         public BookAddCommand(LibraryService libraryService) |             Random.Shared.Next(0, 999), | ||||||
|         { |             Random.Shared.Next(0, 99), | ||||||
|             _libraryService = libraryService; |             Random.Shared.Next(0, 99999), | ||||||
|         } |             Random.Shared.Next(0, 99), | ||||||
|  |             Random.Shared.Next(0, 9) | ||||||
|  |         ); | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) |     public ValueTask ExecuteAsync(IConsole console) | ||||||
|     { |     { | ||||||
|             if (_libraryService.GetBook(Title) != null) |         if (libraryProvider.TryGetBook(Title) is not null) | ||||||
|                 throw new CommandException("Book already exists.", 1); |             throw new CommandException($"Book '{Title}' already exists.", 10); | ||||||
|  |  | ||||||
|         var book = new Book(Title, Author, Published, Isbn); |         var book = new Book(Title, Author, Published, Isbn); | ||||||
|             _libraryService.AddBook(book); |         libraryProvider.AddBook(book); | ||||||
|  |  | ||||||
|             console.Output.WriteLine("Book added."); |         console.WriteLine($"Book '{Title}' added."); | ||||||
|             console.RenderBook(book); |         console.WriteBook(book); | ||||||
|  |  | ||||||
|         return default; |         return default; | ||||||
|     } |     } | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public partial class BookAddCommand |  | ||||||
|     { |  | ||||||
|         private static readonly Random Random = new Random(); |  | ||||||
|  |  | ||||||
|         private static DateTimeOffset CreateRandomDate() => new DateTimeOffset( |  | ||||||
|             Random.Next(1800, 2020), |  | ||||||
|             Random.Next(1, 12), |  | ||||||
|             Random.Next(1, 28), |  | ||||||
|             Random.Next(1, 23), |  | ||||||
|             Random.Next(1, 59), |  | ||||||
|             Random.Next(1, 59), |  | ||||||
|             TimeSpan.Zero); |  | ||||||
|  |  | ||||||
|         private static Isbn CreateRandomIsbn() => new Isbn( |  | ||||||
|             Random.Next(0, 999), |  | ||||||
|             Random.Next(0, 99), |  | ||||||
|             Random.Next(0, 99999), |  | ||||||
|             Random.Next(0, 99), |  | ||||||
|             Random.Next(0, 9)); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| @@ -1,34 +1,27 @@ | |||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| using CliFx.Attributes; | using CliFx.Attributes; | ||||||
| using CliFx.Demo.Internal; | using CliFx.Demo.Domain; | ||||||
| using CliFx.Demo.Services; | using CliFx.Demo.Utils; | ||||||
| using CliFx.Exceptions; | using CliFx.Exceptions; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Commands | namespace CliFx.Demo.Commands; | ||||||
|  |  | ||||||
|  | [Command("book", Description = "Retrieves a book from the library.")] | ||||||
|  | public class BookCommand(LibraryProvider libraryProvider) : ICommand | ||||||
| { | { | ||||||
|     [Command("book", Description = "View, list, add or remove books.")] |     [CommandParameter(0, Name = "title", Description = "Title of the book to retrieve.")] | ||||||
|     public class BookCommand : ICommand |     public required string Title { get; init; } | ||||||
|     { |  | ||||||
|         private readonly LibraryService _libraryService; |  | ||||||
|  |  | ||||||
|         [CommandParameter(0, Name = "title", Description = "Book title.")] |  | ||||||
|         public string Title { get; set; } = ""; |  | ||||||
|  |  | ||||||
|         public BookCommand(LibraryService libraryService) |  | ||||||
|         { |  | ||||||
|             _libraryService = libraryService; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) |     public ValueTask ExecuteAsync(IConsole console) | ||||||
|     { |     { | ||||||
|             var book = _libraryService.GetBook(Title); |         var book = libraryProvider.TryGetBook(Title); | ||||||
|  |  | ||||||
|             if (book == null) |         if (book is null) | ||||||
|                 throw new CommandException("Book not found.", 1); |             throw new CommandException($"Book '{Title}' not found.", 10); | ||||||
|  |  | ||||||
|             console.RenderBook(book); |         console.WriteBook(book); | ||||||
|  |  | ||||||
|         return default; |         return default; | ||||||
|     } |     } | ||||||
|     } |  | ||||||
| } | } | ||||||
| @@ -1,37 +1,29 @@ | |||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| using CliFx.Attributes; | using CliFx.Attributes; | ||||||
| using CliFx.Demo.Internal; | using CliFx.Demo.Domain; | ||||||
| using CliFx.Demo.Services; | using CliFx.Demo.Utils; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Commands | namespace CliFx.Demo.Commands; | ||||||
|  |  | ||||||
|  | [Command("book list", Description = "Lists all books in the library.")] | ||||||
|  | public class BookListCommand(LibraryProvider libraryProvider) : ICommand | ||||||
| { | { | ||||||
|     [Command("book list", Description = "List all books in the library.")] |  | ||||||
|     public class BookListCommand : ICommand |  | ||||||
|     { |  | ||||||
|         private readonly LibraryService _libraryService; |  | ||||||
|  |  | ||||||
|         public BookListCommand(LibraryService libraryService) |  | ||||||
|         { |  | ||||||
|             _libraryService = libraryService; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) |     public ValueTask ExecuteAsync(IConsole console) | ||||||
|     { |     { | ||||||
|             var library = _libraryService.GetLibrary(); |         var library = libraryProvider.GetLibrary(); | ||||||
|  |  | ||||||
|             var isFirst = true; |         for (var i = 0; i < library.Books.Count; i++) | ||||||
|             foreach (var book in library.Books) |  | ||||||
|         { |         { | ||||||
|                 // Margin |             // Add margin | ||||||
|                 if (!isFirst) |             if (i != 0) | ||||||
|                     console.Output.WriteLine(); |                 console.WriteLine(); | ||||||
|                 isFirst = false; |  | ||||||
|  |  | ||||||
|             // Render book |             // Render book | ||||||
|                 console.RenderBook(book); |             var book = library.Books[i]; | ||||||
|  |             console.WriteBook(book); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         return default; |         return default; | ||||||
|     } |     } | ||||||
|     } |  | ||||||
| } | } | ||||||
| @@ -1,35 +1,28 @@ | |||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| using CliFx.Attributes; | using CliFx.Attributes; | ||||||
| using CliFx.Demo.Services; | using CliFx.Demo.Domain; | ||||||
| using CliFx.Exceptions; | using CliFx.Exceptions; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Commands | namespace CliFx.Demo.Commands; | ||||||
|  |  | ||||||
|  | [Command("book remove", Description = "Removes a book from the library.")] | ||||||
|  | public class BookRemoveCommand(LibraryProvider libraryProvider) : ICommand | ||||||
| { | { | ||||||
|     [Command("book remove", Description = "Remove a book from the library.")] |     [CommandParameter(0, Name = "title", Description = "Title of the book to remove.")] | ||||||
|     public class BookRemoveCommand : ICommand |     public required string Title { get; init; } | ||||||
|     { |  | ||||||
|         private readonly LibraryService _libraryService; |  | ||||||
|  |  | ||||||
|         [CommandParameter(0, Name = "title", Description = "Book title.")] |  | ||||||
|         public string Title { get; set; } = ""; |  | ||||||
|  |  | ||||||
|         public BookRemoveCommand(LibraryService libraryService) |  | ||||||
|         { |  | ||||||
|             _libraryService = libraryService; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) |     public ValueTask ExecuteAsync(IConsole console) | ||||||
|     { |     { | ||||||
|             var book = _libraryService.GetBook(Title); |         var book = libraryProvider.TryGetBook(Title); | ||||||
|  |  | ||||||
|             if (book == null) |         if (book is null) | ||||||
|                 throw new CommandException("Book not found.", 1); |             throw new CommandException($"Book '{Title}' not found.", 10); | ||||||
|  |  | ||||||
|             _libraryService.RemoveBook(book); |         libraryProvider.RemoveBook(book); | ||||||
|  |  | ||||||
|             console.Output.WriteLine($"Book {Title} removed."); |         console.WriteLine($"Book '{Title}' removed."); | ||||||
|  |  | ||||||
|         return default; |         return default; | ||||||
|     } |     } | ||||||
|     } |  | ||||||
| } | } | ||||||
							
								
								
									
										5
									
								
								CliFx.Demo/Domain/Book.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								CliFx.Demo/Domain/Book.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | |||||||
|  | using System; | ||||||
|  |  | ||||||
|  | namespace CliFx.Demo.Domain; | ||||||
|  |  | ||||||
|  | public record Book(string Title, string Author, DateTimeOffset Published, Isbn Isbn); | ||||||
							
								
								
									
										31
									
								
								CliFx.Demo/Domain/Isbn.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								CliFx.Demo/Domain/Isbn.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,31 @@ | |||||||
|  | using System; | ||||||
|  |  | ||||||
|  | namespace CliFx.Demo.Domain; | ||||||
|  |  | ||||||
|  | public partial record Isbn( | ||||||
|  |     int EanPrefix, | ||||||
|  |     int RegistrationGroup, | ||||||
|  |     int Registrant, | ||||||
|  |     int Publication, | ||||||
|  |     int CheckDigit | ||||||
|  | ) | ||||||
|  | { | ||||||
|  |     public override string ToString() => | ||||||
|  |         $"{EanPrefix:000}-{RegistrationGroup:00}-{Registrant:00000}-{Publication:00}-{CheckDigit:0}"; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | public partial record Isbn | ||||||
|  | { | ||||||
|  |     public static Isbn Parse(string value, IFormatProvider formatProvider) | ||||||
|  |     { | ||||||
|  |         var components = value.Split('-', 5, StringSplitOptions.RemoveEmptyEntries); | ||||||
|  |  | ||||||
|  |         return new Isbn( | ||||||
|  |             int.Parse(components[0], formatProvider), | ||||||
|  |             int.Parse(components[1], formatProvider), | ||||||
|  |             int.Parse(components[2], formatProvider), | ||||||
|  |             int.Parse(components[3], formatProvider), | ||||||
|  |             int.Parse(components[4], formatProvider) | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										27
									
								
								CliFx.Demo/Domain/Library.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								CliFx.Demo/Domain/Library.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Linq; | ||||||
|  |  | ||||||
|  | namespace CliFx.Demo.Domain; | ||||||
|  |  | ||||||
|  | public partial record Library(IReadOnlyList<Book> Books) | ||||||
|  | { | ||||||
|  |     public Library WithBook(Book book) | ||||||
|  |     { | ||||||
|  |         var books = Books.ToList(); | ||||||
|  |         books.Add(book); | ||||||
|  |  | ||||||
|  |         return new Library(books); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Library WithoutBook(Book book) | ||||||
|  |     { | ||||||
|  |         var books = Books.Where(b => b != book).ToArray(); | ||||||
|  |  | ||||||
|  |         return new Library(books); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
|  | public partial record Library | ||||||
|  | { | ||||||
|  |     public static Library Empty { get; } = new([]); | ||||||
|  | } | ||||||
							
								
								
									
										6
									
								
								CliFx.Demo/Domain/LibraryJsonContext.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								CliFx.Demo/Domain/LibraryJsonContext.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | |||||||
|  | using System.Text.Json.Serialization; | ||||||
|  |  | ||||||
|  | namespace CliFx.Demo.Domain; | ||||||
|  |  | ||||||
|  | [JsonSerializable(typeof(Library))] | ||||||
|  | public partial class LibraryJsonContext : JsonSerializerContext; | ||||||
							
								
								
									
										43
									
								
								CliFx.Demo/Domain/LibraryProvider.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								CliFx.Demo/Domain/LibraryProvider.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | |||||||
|  | using System.IO; | ||||||
|  | using System.Linq; | ||||||
|  | using System.Text.Json; | ||||||
|  |  | ||||||
|  | namespace CliFx.Demo.Domain; | ||||||
|  |  | ||||||
|  | public class LibraryProvider | ||||||
|  | { | ||||||
|  |     private static string StorageFilePath { get; } = | ||||||
|  |         Path.Combine(Directory.GetCurrentDirectory(), "Library.json"); | ||||||
|  |  | ||||||
|  |     private void StoreLibrary(Library library) | ||||||
|  |     { | ||||||
|  |         var data = JsonSerializer.Serialize(library, LibraryJsonContext.Default.Library); | ||||||
|  |         File.WriteAllText(StorageFilePath, data); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Library GetLibrary() | ||||||
|  |     { | ||||||
|  |         if (!File.Exists(StorageFilePath)) | ||||||
|  |             return Library.Empty; | ||||||
|  |  | ||||||
|  |         var data = File.ReadAllText(StorageFilePath); | ||||||
|  |  | ||||||
|  |         return JsonSerializer.Deserialize(data, LibraryJsonContext.Default.Library) | ||||||
|  |             ?? Library.Empty; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Book? TryGetBook(string title) => | ||||||
|  |         GetLibrary().Books.FirstOrDefault(b => b.Title == title); | ||||||
|  |  | ||||||
|  |     public void AddBook(Book book) | ||||||
|  |     { | ||||||
|  |         var updatedLibrary = GetLibrary().WithBook(book); | ||||||
|  |         StoreLibrary(updatedLibrary); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public void RemoveBook(Book book) | ||||||
|  |     { | ||||||
|  |         var updatedLibrary = GetLibrary().WithoutBook(book); | ||||||
|  |         StoreLibrary(updatedLibrary); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,29 +0,0 @@ | |||||||
| using System; |  | ||||||
| using CliFx.Demo.Models; |  | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Internal |  | ||||||
| { |  | ||||||
|     internal static class Extensions |  | ||||||
|     { |  | ||||||
|         public static void RenderBook(this IConsole console, Book book) |  | ||||||
|         { |  | ||||||
|             // Title |  | ||||||
|             console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine(book.Title)); |  | ||||||
|  |  | ||||||
|             // Author |  | ||||||
|             console.Output.Write("  "); |  | ||||||
|             console.Output.Write("Author: "); |  | ||||||
|             console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine(book.Author)); |  | ||||||
|  |  | ||||||
|             // Published |  | ||||||
|             console.Output.Write("  "); |  | ||||||
|             console.Output.Write("Published: "); |  | ||||||
|             console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine($"{book.Published:d}")); |  | ||||||
|  |  | ||||||
|             // ISBN |  | ||||||
|             console.Output.Write("  "); |  | ||||||
|             console.Output.Write("ISBN: "); |  | ||||||
|             console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine(book.Isbn)); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,23 +0,0 @@ | |||||||
| using System; |  | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Models |  | ||||||
| { |  | ||||||
|     public class Book |  | ||||||
|     { |  | ||||||
|         public string Title { get; } |  | ||||||
|  |  | ||||||
|         public string Author { get; } |  | ||||||
|  |  | ||||||
|         public DateTimeOffset Published { get; } |  | ||||||
|  |  | ||||||
|         public Isbn Isbn { get; } |  | ||||||
|  |  | ||||||
|         public Book(string title, string author, DateTimeOffset published, Isbn isbn) |  | ||||||
|         { |  | ||||||
|             Title = title; |  | ||||||
|             Author = author; |  | ||||||
|             Published = published; |  | ||||||
|             Isbn = isbn; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,22 +0,0 @@ | |||||||
| using System.Linq; |  | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Models |  | ||||||
| { |  | ||||||
|     public static class Extensions |  | ||||||
|     { |  | ||||||
|         public static Library WithBook(this Library library, Book book) |  | ||||||
|         { |  | ||||||
|             var books = library.Books.ToList(); |  | ||||||
|             books.Add(book); |  | ||||||
|  |  | ||||||
|             return new Library(books); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static Library WithoutBook(this Library library, Book book) |  | ||||||
|         { |  | ||||||
|             var books = library.Books.Where(b => b != book).ToArray(); |  | ||||||
|  |  | ||||||
|             return new Library(books); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,45 +0,0 @@ | |||||||
| using System; |  | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Models |  | ||||||
| { |  | ||||||
|     public partial class Isbn |  | ||||||
|     { |  | ||||||
|         public int EanPrefix { get; } |  | ||||||
|  |  | ||||||
|         public int RegistrationGroup { get; } |  | ||||||
|  |  | ||||||
|         public int Registrant { get; } |  | ||||||
|  |  | ||||||
|         public int Publication { get; } |  | ||||||
|  |  | ||||||
|         public int CheckDigit { get; } |  | ||||||
|  |  | ||||||
|         public Isbn(int eanPrefix, int registrationGroup, int registrant, int publication, int checkDigit) |  | ||||||
|         { |  | ||||||
|             EanPrefix = eanPrefix; |  | ||||||
|             RegistrationGroup = registrationGroup; |  | ||||||
|             Registrant = registrant; |  | ||||||
|             Publication = publication; |  | ||||||
|             CheckDigit = checkDigit; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public override string ToString() => |  | ||||||
|             $"{EanPrefix:000}-{RegistrationGroup:00}-{Registrant:00000}-{Publication:00}-{CheckDigit:0}"; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public partial class Isbn |  | ||||||
|     { |  | ||||||
|         public static Isbn Parse(string value, IFormatProvider formatProvider) |  | ||||||
|         { |  | ||||||
|             var components = value.Split('-', 5, StringSplitOptions.RemoveEmptyEntries); |  | ||||||
|  |  | ||||||
|             return new Isbn( |  | ||||||
|                 int.Parse(components[0], formatProvider), |  | ||||||
|                 int.Parse(components[1], formatProvider), |  | ||||||
|                 int.Parse(components[2], formatProvider), |  | ||||||
|                 int.Parse(components[3], formatProvider), |  | ||||||
|                 int.Parse(components[4], formatProvider) |  | ||||||
|             ); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,20 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Collections.Generic; |  | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Models |  | ||||||
| { |  | ||||||
|     public partial class Library |  | ||||||
|     { |  | ||||||
|         public IReadOnlyList<Book> Books { get; } |  | ||||||
|  |  | ||||||
|         public Library(IReadOnlyList<Book> books) |  | ||||||
|         { |  | ||||||
|             Books = books; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public partial class Library |  | ||||||
|     { |  | ||||||
|         public static Library Empty { get; } = new Library(Array.Empty<Book>()); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,35 +1,18 @@ | |||||||
| using System; | using CliFx; | ||||||
| using System.Threading.Tasks; | using CliFx.Demo.Domain; | ||||||
| using CliFx.Demo.Commands; |  | ||||||
| using CliFx.Demo.Services; |  | ||||||
| using Microsoft.Extensions.DependencyInjection; | using Microsoft.Extensions.DependencyInjection; | ||||||
|  |  | ||||||
| namespace CliFx.Demo | // We use Microsoft.Extensions.DependencyInjection for injecting dependencies in commands | ||||||
| { | var services = new ServiceCollection(); | ||||||
|     public static class Program | services.AddSingleton<LibraryProvider>(); | ||||||
|     { |  | ||||||
|         private static IServiceProvider GetServiceProvider() |  | ||||||
|         { |  | ||||||
|             // We use Microsoft.Extensions.DependencyInjection for injecting dependencies in commands |  | ||||||
|             var services = new ServiceCollection(); |  | ||||||
|  |  | ||||||
|             // Register services | // Register all commands as transient services | ||||||
|             services.AddSingleton<LibraryService>(); | foreach (var commandType in commandTypes) | ||||||
|  |     services.AddTransient(commandType); | ||||||
|  |  | ||||||
|             // Register commands | return await new CliApplicationBuilder() | ||||||
|             services.AddTransient<BookCommand>(); |     .SetDescription("Demo application showcasing CliFx features.") | ||||||
|             services.AddTransient<BookAddCommand>(); |  | ||||||
|             services.AddTransient<BookRemoveCommand>(); |  | ||||||
|             services.AddTransient<BookListCommand>(); |  | ||||||
|  |  | ||||||
|             return services.BuildServiceProvider(); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static async Task<int> Main() => |  | ||||||
|             await new CliApplicationBuilder() |  | ||||||
|     .AddCommandsFromThisAssembly() |     .AddCommandsFromThisAssembly() | ||||||
|                 .UseTypeActivator(GetServiceProvider().GetService) |     .UseTypeActivator(services.BuildServiceProvider()) | ||||||
|     .Build() |     .Build() | ||||||
|     .RunAsync(); |     .RunAsync(); | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,7 +1,5 @@ | |||||||
| # CliFx Demo Project | # CliFx Demo Project | ||||||
|  |  | ||||||
| Sample command line interface for managing a library of books. | Sample command-line interface for managing a library of books. | ||||||
|  |  | ||||||
| This demo project shows basic CliFx functionality such as command routing, argument parsing, autogenerated help text, and some other things. | This demo project showcases basic CliFx functionality such as command routing, argument parsing, and autogenerated help text. | ||||||
|  |  | ||||||
| You can get a list of available commands by running `CliFx.Demo --help`. |  | ||||||
|   | |||||||
| @@ -1,42 +0,0 @@ | |||||||
| using System.IO; |  | ||||||
| using System.Linq; |  | ||||||
| using CliFx.Demo.Models; |  | ||||||
| using Newtonsoft.Json; |  | ||||||
|  |  | ||||||
| namespace CliFx.Demo.Services |  | ||||||
| { |  | ||||||
|     public class LibraryService |  | ||||||
|     { |  | ||||||
|         private string StorageFilePath => Path.Combine(Directory.GetCurrentDirectory(), "Data.json"); |  | ||||||
|  |  | ||||||
|         private void StoreLibrary(Library library) |  | ||||||
|         { |  | ||||||
|             var data = JsonConvert.SerializeObject(library); |  | ||||||
|             File.WriteAllText(StorageFilePath, data); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public Library GetLibrary() |  | ||||||
|         { |  | ||||||
|             if (!File.Exists(StorageFilePath)) |  | ||||||
|                 return Library.Empty; |  | ||||||
|  |  | ||||||
|             var data = File.ReadAllText(StorageFilePath); |  | ||||||
|  |  | ||||||
|             return JsonConvert.DeserializeObject<Library>(data); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public Book? GetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title); |  | ||||||
|  |  | ||||||
|         public void AddBook(Book book) |  | ||||||
|         { |  | ||||||
|             var updatedLibrary = GetLibrary().WithBook(book); |  | ||||||
|             StoreLibrary(updatedLibrary); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public void RemoveBook(Book book) |  | ||||||
|         { |  | ||||||
|             var updatedLibrary = GetLibrary().WithoutBook(book); |  | ||||||
|             StoreLibrary(updatedLibrary); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
							
								
								
									
										39
									
								
								CliFx.Demo/Utils/ConsoleExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								CliFx.Demo/Utils/ConsoleExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | |||||||
|  | using System; | ||||||
|  | using CliFx.Demo.Domain; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  |  | ||||||
|  | namespace CliFx.Demo.Utils; | ||||||
|  |  | ||||||
|  | internal static class ConsoleExtensions | ||||||
|  | { | ||||||
|  |     public static void WriteBook(this ConsoleWriter writer, Book book) | ||||||
|  |     { | ||||||
|  |         // Title | ||||||
|  |         using (writer.Console.WithForegroundColor(ConsoleColor.White)) | ||||||
|  |             writer.WriteLine(book.Title); | ||||||
|  |  | ||||||
|  |         // Author | ||||||
|  |         writer.Write("  "); | ||||||
|  |         writer.Write("Author: "); | ||||||
|  |  | ||||||
|  |         using (writer.Console.WithForegroundColor(ConsoleColor.White)) | ||||||
|  |             writer.WriteLine(book.Author); | ||||||
|  |  | ||||||
|  |         // Published | ||||||
|  |         writer.Write("  "); | ||||||
|  |         writer.Write("Published: "); | ||||||
|  |  | ||||||
|  |         using (writer.Console.WithForegroundColor(ConsoleColor.White)) | ||||||
|  |             writer.WriteLine($"{book.Published:d}"); | ||||||
|  |  | ||||||
|  |         // ISBN | ||||||
|  |         writer.Write("  "); | ||||||
|  |         writer.Write("ISBN: "); | ||||||
|  |  | ||||||
|  |         using (writer.Console.WithForegroundColor(ConsoleColor.White)) | ||||||
|  |             writer.WriteLine(book.Isbn); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static void WriteBook(this IConsole console, Book book) => | ||||||
|  |         console.Output.WriteBook(book); | ||||||
|  | } | ||||||
							
								
								
									
										27
									
								
								CliFx.SourceGeneration/CliFx.SourceGeneration.csproj
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								CliFx.SourceGeneration/CliFx.SourceGeneration.csproj
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | <Project Sdk="Microsoft.NET.Sdk"> | ||||||
|  |   <PropertyGroup> | ||||||
|  |     <TargetFramework>netstandard2.0</TargetFramework> | ||||||
|  |     <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> | ||||||
|  |     <GenerateDependencyFile>true</GenerateDependencyFile> | ||||||
|  |     <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||||||
|  |     <NoWarn>$(NoWarn);RS1035</NoWarn> | ||||||
|  |   </PropertyGroup> | ||||||
|  |   <PropertyGroup> | ||||||
|  |     <!-- | ||||||
|  |       Because this project only has a single target framework, the condition in | ||||||
|  |       Directory.Build.props does not appear to work. This is a workaround for that. | ||||||
|  |     --> | ||||||
|  |     <Nullable>annotations</Nullable> | ||||||
|  |   </PropertyGroup> | ||||||
|  |   <ItemGroup> | ||||||
|  |     <PackageReference Include="CSharpier.MsBuild" Version="1.1.2" PrivateAssets="all" /> | ||||||
|  |     <!-- Make sure to target the lowest possible version of the compiler for wider support --> | ||||||
|  |     <PackageReference Include="Microsoft.CodeAnalysis" Version="4.11.0" PrivateAssets="all" /> | ||||||
|  |     <PackageReference | ||||||
|  |       Include="Microsoft.CodeAnalysis.CSharp" | ||||||
|  |       Version="4.11.0" | ||||||
|  |       PrivateAssets="all" | ||||||
|  |     /> | ||||||
|  |     <PackageReference Include="PolyShim" Version="1.15.0" PrivateAssets="all" /> | ||||||
|  |   </ItemGroup> | ||||||
|  | </Project> | ||||||
							
								
								
									
										130
									
								
								CliFx.SourceGeneration/CommandSchemaGenerator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										130
									
								
								CliFx.SourceGeneration/CommandSchemaGenerator.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,130 @@ | |||||||
|  | using System.Linq; | ||||||
|  | using CliFx.SourceGeneration.SemanticModel; | ||||||
|  | using CliFx.SourceGeneration.Utils.Extensions; | ||||||
|  | using Microsoft.CodeAnalysis; | ||||||
|  | using Microsoft.CodeAnalysis.CSharp; | ||||||
|  | using Microsoft.CodeAnalysis.CSharp.Syntax; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration; | ||||||
|  |  | ||||||
|  | [Generator] | ||||||
|  | public class CommandSchemaGenerator : IIncrementalGenerator | ||||||
|  | { | ||||||
|  |     public void Initialize(IncrementalGeneratorInitializationContext context) | ||||||
|  |     { | ||||||
|  |         var values = context.SyntaxProvider.ForAttributeWithMetadataName<( | ||||||
|  |             CommandSymbol?, | ||||||
|  |             Diagnostic? | ||||||
|  |         )>( | ||||||
|  |             KnownSymbolNames.CliFxCommandAttribute, | ||||||
|  |             (n, _) => n is TypeDeclarationSyntax, | ||||||
|  |             (x, _) => | ||||||
|  |             { | ||||||
|  |                 // Predicate above ensures that these casts are safe | ||||||
|  |                 var commandTypeSyntax = (TypeDeclarationSyntax)x.TargetNode; | ||||||
|  |                 var commandTypeSymbol = (INamedTypeSymbol)x.TargetSymbol; | ||||||
|  |  | ||||||
|  |                 // Check if the target type and all its containing types are partial | ||||||
|  |                 if ( | ||||||
|  |                     commandTypeSyntax | ||||||
|  |                         .AncestorsAndSelf() | ||||||
|  |                         .Any(a => | ||||||
|  |                             a is TypeDeclarationSyntax t | ||||||
|  |                             && !t.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)) | ||||||
|  |                         ) | ||||||
|  |                 ) | ||||||
|  |                 { | ||||||
|  |                     return ( | ||||||
|  |                         null, | ||||||
|  |                         Diagnostic.Create( | ||||||
|  |                             DiagnosticDescriptors.CommandMustBePartial, | ||||||
|  |                             commandTypeSyntax.Identifier.GetLocation() | ||||||
|  |                         ) | ||||||
|  |                     ); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 // Check if the target type implements ICommand | ||||||
|  |                 var hasCommandInterface = commandTypeSymbol.AllInterfaces.Any(i => | ||||||
|  |                     i.DisplayNameMatches(KnownSymbolNames.CliFxCommandInterface) | ||||||
|  |                 ); | ||||||
|  |  | ||||||
|  |                 if (!hasCommandInterface) | ||||||
|  |                 { | ||||||
|  |                     return ( | ||||||
|  |                         null, | ||||||
|  |                         Diagnostic.Create( | ||||||
|  |                             DiagnosticDescriptors.CommandMustImplementInterface, | ||||||
|  |                             commandTypeSymbol.Locations.First() | ||||||
|  |                         ) | ||||||
|  |                     ); | ||||||
|  |                 } | ||||||
|  |  | ||||||
|  |                 // Resolve the command | ||||||
|  |                 var commandAttribute = x.Attributes.First(a => | ||||||
|  |                     a.AttributeClass?.DisplayNameMatches(KnownSymbolNames.CliFxCommandAttribute) | ||||||
|  |                     == true | ||||||
|  |                 ); | ||||||
|  |  | ||||||
|  |                 var command = CommandSymbol.FromSymbol(commandTypeSymbol, commandAttribute); | ||||||
|  |  | ||||||
|  |                 // TODO: validate command | ||||||
|  |  | ||||||
|  |                 return (command, null); | ||||||
|  |             } | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Report diagnostics | ||||||
|  |         var diagnostics = values.Select((v, _) => v.Item2).WhereNotNull(); | ||||||
|  |         context.RegisterSourceOutput(diagnostics, (x, d) => x.ReportDiagnostic(d)); | ||||||
|  |  | ||||||
|  |         // Generate command schemas | ||||||
|  |         var symbols = values.Select((v, _) => v.Item1).WhereNotNull(); | ||||||
|  |         context.RegisterSourceOutput( | ||||||
|  |             symbols, | ||||||
|  |             (x, c) => | ||||||
|  |                 x.AddSource( | ||||||
|  |                     $"{c.Type.FullyQualifiedName}.CommandSchema.Generated.cs", | ||||||
|  |                     // lang=csharp | ||||||
|  |                     $$""" | ||||||
|  |                     namespace {{c.Type.Namespace}}; | ||||||
|  |  | ||||||
|  |                     partial class {{c.Type.Name}} | ||||||
|  |                     { | ||||||
|  |                         public static CliFx.Schema.CommandSchema<{{c.Type.FullyQualifiedName}}> Schema { get; } = {{c.GenerateSchemaInitializationCode()}}; | ||||||
|  |                     } | ||||||
|  |                     """ | ||||||
|  |                 ) | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Generate extension methods | ||||||
|  |         var symbolsCollected = symbols.Collect(); | ||||||
|  |         context.RegisterSourceOutput( | ||||||
|  |             symbolsCollected, | ||||||
|  |             (x, cs) => | ||||||
|  |                 x.AddSource( | ||||||
|  |                     "CommandSchemaExtensions.Generated.cs", | ||||||
|  |                     // lang=csharp | ||||||
|  |                     $$""" | ||||||
|  |                   namespace CliFx; | ||||||
|  |  | ||||||
|  |                   static partial class GeneratedExtensions | ||||||
|  |                   { | ||||||
|  |                       public static CliFx.CliApplicationBuilder AddCommandsFromThisAssembly(this CliFx.CliApplicationBuilder builder) | ||||||
|  |                       { | ||||||
|  |                           {{ | ||||||
|  |                               cs.Select(c => c.Type.FullyQualifiedName) | ||||||
|  |                                   .Select(t => | ||||||
|  |                                       // lang=csharp | ||||||
|  |                                       $"builder.AddCommand({t}.Schema);" | ||||||
|  |                                   ) | ||||||
|  |                                   .JoinToString("\n") | ||||||
|  |                           }} | ||||||
|  |                            | ||||||
|  |                           return builder; | ||||||
|  |                       } | ||||||
|  |                   } | ||||||
|  |                   """ | ||||||
|  |                 ) | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										27
									
								
								CliFx.SourceGeneration/DiagnosticDescriptors.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								CliFx.SourceGeneration/DiagnosticDescriptors.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | using CliFx.SourceGeneration.SemanticModel; | ||||||
|  | using Microsoft.CodeAnalysis; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration; | ||||||
|  |  | ||||||
|  | internal static class DiagnosticDescriptors | ||||||
|  | { | ||||||
|  |     public static DiagnosticDescriptor CommandMustBePartial { get; } = | ||||||
|  |         new( | ||||||
|  |             $"{nameof(CliFx)}_{nameof(CommandMustBePartial)}", | ||||||
|  |             "Command types must be declared as `partial`", | ||||||
|  |             "This type (and all its containing types, if present) must be declared as `partial` in order to be a valid command.", | ||||||
|  |             "CliFx", | ||||||
|  |             DiagnosticSeverity.Error, | ||||||
|  |             true | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |     public static DiagnosticDescriptor CommandMustImplementInterface { get; } = | ||||||
|  |         new( | ||||||
|  |             $"{nameof(CliFx)}_{nameof(CommandMustImplementInterface)}", | ||||||
|  |             $"Commands must implement the `{KnownSymbolNames.CliFxCommandInterface}` interface", | ||||||
|  |             $"This type must implement the `{KnownSymbolNames.CliFxCommandInterface}` interface in order to be a valid command.", | ||||||
|  |             "CliFx", | ||||||
|  |             DiagnosticSeverity.Error, | ||||||
|  |             true | ||||||
|  |         ); | ||||||
|  | } | ||||||
							
								
								
									
										66
									
								
								CliFx.SourceGeneration/SemanticModel/CommandInputSymbol.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								CliFx.SourceGeneration/SemanticModel/CommandInputSymbol.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,66 @@ | |||||||
|  | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Linq; | ||||||
|  | using Microsoft.CodeAnalysis; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.SemanticModel; | ||||||
|  |  | ||||||
|  | internal abstract partial class CommandInputSymbol( | ||||||
|  |     PropertyDescriptor property, | ||||||
|  |     bool isSequence, | ||||||
|  |     string? description, | ||||||
|  |     TypeDescriptor? converterType, | ||||||
|  |     IReadOnlyList<TypeDescriptor> validatorTypes | ||||||
|  | ) | ||||||
|  | { | ||||||
|  |     public PropertyDescriptor Property { get; } = property; | ||||||
|  |  | ||||||
|  |     public bool IsSequence { get; } = isSequence; | ||||||
|  |  | ||||||
|  |     public string? Description { get; } = description; | ||||||
|  |  | ||||||
|  |     public TypeDescriptor? ConverterType { get; } = converterType; | ||||||
|  |  | ||||||
|  |     public IReadOnlyList<TypeDescriptor> ValidatorTypes { get; } = validatorTypes; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class CommandInputSymbol : IEquatable<CommandInputSymbol> | ||||||
|  | { | ||||||
|  |     public bool Equals(CommandInputSymbol? other) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, other)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, other)) | ||||||
|  |             return true; | ||||||
|  |  | ||||||
|  |         return Property.Equals(other.Property) | ||||||
|  |             && IsSequence == other.IsSequence | ||||||
|  |             && Description == other.Description | ||||||
|  |             && Equals(ConverterType, other.ConverterType) | ||||||
|  |             && ValidatorTypes.SequenceEqual(other.ValidatorTypes); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override bool Equals(object? obj) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, obj)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, obj)) | ||||||
|  |             return true; | ||||||
|  |         if (obj.GetType() != GetType()) | ||||||
|  |             return false; | ||||||
|  |  | ||||||
|  |         return Equals((CommandInputSymbol)obj); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override int GetHashCode() => | ||||||
|  |         HashCode.Combine(Property, IsSequence, Description, ConverterType, ValidatorTypes); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class CommandInputSymbol | ||||||
|  | { | ||||||
|  |     public static bool IsSequenceType(ITypeSymbol type) => | ||||||
|  |         type.AllInterfaces.Any(i => | ||||||
|  |             i.SpecialType == SpecialType.System_Collections_Generic_IEnumerable_T | ||||||
|  |         ) | ||||||
|  |         && type.SpecialType != SpecialType.System_String; | ||||||
|  | } | ||||||
							
								
								
									
										90
									
								
								CliFx.SourceGeneration/SemanticModel/CommandOptionSymbol.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								CliFx.SourceGeneration/SemanticModel/CommandOptionSymbol.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | |||||||
|  | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Linq; | ||||||
|  | using CliFx.SourceGeneration.Utils.Extensions; | ||||||
|  | using Microsoft.CodeAnalysis; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.SemanticModel; | ||||||
|  |  | ||||||
|  | internal partial class CommandOptionSymbol( | ||||||
|  |     PropertyDescriptor property, | ||||||
|  |     bool isSequence, | ||||||
|  |     string? name, | ||||||
|  |     char? shortName, | ||||||
|  |     string? environmentVariable, | ||||||
|  |     bool isRequired, | ||||||
|  |     string? description, | ||||||
|  |     TypeDescriptor? converterType, | ||||||
|  |     IReadOnlyList<TypeDescriptor> validatorTypes | ||||||
|  | ) : CommandInputSymbol(property, isSequence, description, converterType, validatorTypes) | ||||||
|  | { | ||||||
|  |     public string? Name { get; } = name; | ||||||
|  |  | ||||||
|  |     public char? ShortName { get; } = shortName; | ||||||
|  |  | ||||||
|  |     public string? EnvironmentVariable { get; } = environmentVariable; | ||||||
|  |  | ||||||
|  |     public bool IsRequired { get; } = isRequired; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class CommandOptionSymbol : IEquatable<CommandOptionSymbol> | ||||||
|  | { | ||||||
|  |     public bool Equals(CommandOptionSymbol? other) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, other)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, other)) | ||||||
|  |             return true; | ||||||
|  |  | ||||||
|  |         return base.Equals(other) | ||||||
|  |             && Name == other.Name | ||||||
|  |             && ShortName == other.ShortName | ||||||
|  |             && EnvironmentVariable == other.EnvironmentVariable | ||||||
|  |             && IsRequired == other.IsRequired; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override bool Equals(object? obj) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, obj)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, obj)) | ||||||
|  |             return true; | ||||||
|  |         if (obj.GetType() != GetType()) | ||||||
|  |             return false; | ||||||
|  |  | ||||||
|  |         return Equals((CommandOptionSymbol)obj); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override int GetHashCode() => | ||||||
|  |         HashCode.Combine(base.GetHashCode(), Name, ShortName, EnvironmentVariable, IsRequired); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class CommandOptionSymbol | ||||||
|  | { | ||||||
|  |     public static CommandOptionSymbol FromSymbol( | ||||||
|  |         IPropertySymbol property, | ||||||
|  |         AttributeData attribute | ||||||
|  |     ) => | ||||||
|  |         new( | ||||||
|  |             PropertyDescriptor.FromSymbol(property), | ||||||
|  |             IsSequenceType(property.Type), | ||||||
|  |             attribute | ||||||
|  |                 .ConstructorArguments.FirstOrDefault(a => | ||||||
|  |                     a.Type?.SpecialType == SpecialType.System_String | ||||||
|  |                 ) | ||||||
|  |                 .Value as string, | ||||||
|  |             attribute | ||||||
|  |                 .ConstructorArguments.FirstOrDefault(a => | ||||||
|  |                     a.Type?.SpecialType == SpecialType.System_Char | ||||||
|  |                 ) | ||||||
|  |                 .Value as char?, | ||||||
|  |             attribute.GetNamedArgumentValue("EnvironmentVariable", default(string)), | ||||||
|  |             attribute.GetNamedArgumentValue("IsRequired", property.IsRequired), | ||||||
|  |             attribute.GetNamedArgumentValue("Description", default(string)), | ||||||
|  |             TypeDescriptor.FromSymbol(attribute.GetNamedArgumentValue<ITypeSymbol?>("Converter")), | ||||||
|  |             attribute | ||||||
|  |                 .GetNamedArgumentValues<ITypeSymbol>("Validators") | ||||||
|  |                 .Select(TypeDescriptor.FromSymbol) | ||||||
|  |                 .ToArray() | ||||||
|  |         ); | ||||||
|  | } | ||||||
| @@ -0,0 +1,77 @@ | |||||||
|  | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Linq; | ||||||
|  | using CliFx.SourceGeneration.Utils.Extensions; | ||||||
|  | using Microsoft.CodeAnalysis; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.SemanticModel; | ||||||
|  |  | ||||||
|  | internal partial class CommandParameterSymbol( | ||||||
|  |     PropertyDescriptor property, | ||||||
|  |     bool isSequence, | ||||||
|  |     int order, | ||||||
|  |     string name, | ||||||
|  |     bool isRequired, | ||||||
|  |     string? description, | ||||||
|  |     TypeDescriptor? converterType, | ||||||
|  |     IReadOnlyList<TypeDescriptor> validatorTypes | ||||||
|  | ) : CommandInputSymbol(property, isSequence, description, converterType, validatorTypes) | ||||||
|  | { | ||||||
|  |     public int Order { get; } = order; | ||||||
|  |  | ||||||
|  |     public string Name { get; } = name; | ||||||
|  |  | ||||||
|  |     public bool IsRequired { get; } = isRequired; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class CommandParameterSymbol : IEquatable<CommandParameterSymbol> | ||||||
|  | { | ||||||
|  |     public bool Equals(CommandParameterSymbol? other) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, other)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, other)) | ||||||
|  |             return true; | ||||||
|  |  | ||||||
|  |         return base.Equals(other) | ||||||
|  |             && Order == other.Order | ||||||
|  |             && Name == other.Name | ||||||
|  |             && IsRequired == other.IsRequired; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override bool Equals(object? obj) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, obj)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, obj)) | ||||||
|  |             return true; | ||||||
|  |         if (obj.GetType() != GetType()) | ||||||
|  |             return false; | ||||||
|  |  | ||||||
|  |         return Equals((CommandParameterSymbol)obj); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override int GetHashCode() => | ||||||
|  |         HashCode.Combine(base.GetHashCode(), Order, Name, IsRequired); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class CommandParameterSymbol | ||||||
|  | { | ||||||
|  |     public static CommandParameterSymbol FromSymbol( | ||||||
|  |         IPropertySymbol property, | ||||||
|  |         AttributeData attribute | ||||||
|  |     ) => | ||||||
|  |         new( | ||||||
|  |             PropertyDescriptor.FromSymbol(property), | ||||||
|  |             IsSequenceType(property.Type), | ||||||
|  |             (int)attribute.ConstructorArguments.First().Value!, | ||||||
|  |             attribute.GetNamedArgumentValue("Name", default(string)), | ||||||
|  |             attribute.GetNamedArgumentValue("IsRequired", true), | ||||||
|  |             attribute.GetNamedArgumentValue("Description", default(string)), | ||||||
|  |             TypeDescriptor.FromSymbol(attribute.GetNamedArgumentValue<ITypeSymbol>("Converter")), | ||||||
|  |             attribute | ||||||
|  |                 .GetNamedArgumentValues<ITypeSymbol>("Validators") | ||||||
|  |                 .Select(TypeDescriptor.FromSymbol) | ||||||
|  |                 .ToArray() | ||||||
|  |         ); | ||||||
|  | } | ||||||
							
								
								
									
										165
									
								
								CliFx.SourceGeneration/SemanticModel/CommandSymbol.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										165
									
								
								CliFx.SourceGeneration/SemanticModel/CommandSymbol.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,165 @@ | |||||||
|  | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Linq; | ||||||
|  | using CliFx.SourceGeneration.Utils.Extensions; | ||||||
|  | using Microsoft.CodeAnalysis; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.SemanticModel; | ||||||
|  |  | ||||||
|  | internal partial class CommandSymbol( | ||||||
|  |     TypeDescriptor type, | ||||||
|  |     string? name, | ||||||
|  |     string? description, | ||||||
|  |     IReadOnlyList<CommandInputSymbol> inputs | ||||||
|  | ) | ||||||
|  | { | ||||||
|  |     public TypeDescriptor Type { get; } = type; | ||||||
|  |  | ||||||
|  |     public string? Name { get; } = name; | ||||||
|  |  | ||||||
|  |     public string? Description { get; } = description; | ||||||
|  |  | ||||||
|  |     public IReadOnlyList<CommandInputSymbol> Inputs { get; } = inputs; | ||||||
|  |  | ||||||
|  |     public IReadOnlyList<CommandParameterSymbol> Parameters => | ||||||
|  |         Inputs.OfType<CommandParameterSymbol>().ToArray(); | ||||||
|  |  | ||||||
|  |     public IReadOnlyList<CommandOptionSymbol> Options => | ||||||
|  |         Inputs.OfType<CommandOptionSymbol>().ToArray(); | ||||||
|  |  | ||||||
|  |     private string GeneratePropertyBindingInitializationCode(PropertyDescriptor property) => | ||||||
|  |         // lang=csharp | ||||||
|  |         $$""" | ||||||
|  |             new CliFx.Schema.PropertyBinding<{{Type.FullyQualifiedName}}, {{property | ||||||
|  |                 .Type | ||||||
|  |                 .FullyQualifiedName}}>( | ||||||
|  |                 (obj) => obj.{{property.Name}}, | ||||||
|  |                 (obj, value) => obj.{{property.Name}} = value | ||||||
|  |             ) | ||||||
|  |             """; | ||||||
|  |  | ||||||
|  |     private string GenerateSchemaInitializationCode(CommandInputSymbol input) => | ||||||
|  |         input switch | ||||||
|  |         { | ||||||
|  |             CommandParameterSymbol parameter => | ||||||
|  |             // lang=csharp | ||||||
|  |             $$""" | ||||||
|  |                     new CliFx.Schema.CommandParameterSchema<{{Type.FullyQualifiedName}}, {{parameter | ||||||
|  |                         .Property | ||||||
|  |                         .Type | ||||||
|  |                         .FullyQualifiedName}}>( | ||||||
|  |                         {{GeneratePropertyBindingInitializationCode(parameter.Property)}}, | ||||||
|  |                         {{parameter.IsSequence}}, | ||||||
|  |                         {{parameter.Order}}, | ||||||
|  |                         "{{parameter.Name}}", | ||||||
|  |                         {{parameter.IsRequired}}, | ||||||
|  |                         "{{parameter.Description}}", | ||||||
|  |                         // TODO, | ||||||
|  |                         // TODO | ||||||
|  |                     ); | ||||||
|  |                     """, | ||||||
|  |             CommandOptionSymbol option => | ||||||
|  |             // lang=csharp | ||||||
|  |             $$""" | ||||||
|  |                     new CliFx.Schema.CommandOptionSchema<{{Type.FullyQualifiedName}}, {{option | ||||||
|  |                         .Property | ||||||
|  |                         .Type | ||||||
|  |                         .FullyQualifiedName}}>( | ||||||
|  |                         {{GeneratePropertyBindingInitializationCode(option.Property)}}, | ||||||
|  |                         {{option.IsSequence}}, | ||||||
|  |                         "{{option.Name}}", | ||||||
|  |                         '{{option.ShortName}}', | ||||||
|  |                         "{{option.EnvironmentVariable}}", | ||||||
|  |                         {{option.IsRequired}}, | ||||||
|  |                         "{{option.Description}}", | ||||||
|  |                         // TODO, | ||||||
|  |                         // TODO | ||||||
|  |                     ); | ||||||
|  |                     """, | ||||||
|  |             _ => throw new ArgumentOutOfRangeException(nameof(input), input, null), | ||||||
|  |         }; | ||||||
|  |  | ||||||
|  |     public string GenerateSchemaInitializationCode() => | ||||||
|  |             // lang=csharp | ||||||
|  |             $$""" | ||||||
|  |             new CliFx.Schema.CommandSchema<{{Type.FullyQualifiedName}}>( | ||||||
|  |                 "{{Name}}", | ||||||
|  |                 "{{Description}}", | ||||||
|  |                 new CliFx.Schema.CommandInputSchema[] | ||||||
|  |                 { | ||||||
|  |                     {{Inputs.Select(GenerateSchemaInitializationCode).JoinToString(",\n")}} | ||||||
|  |                 } | ||||||
|  |             ) | ||||||
|  |             """; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class CommandSymbol : IEquatable<CommandSymbol> | ||||||
|  | { | ||||||
|  |     public bool Equals(CommandSymbol? other) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, other)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, other)) | ||||||
|  |             return true; | ||||||
|  |  | ||||||
|  |         return Type.Equals(other.Type) | ||||||
|  |             && Name == other.Name | ||||||
|  |             && Description == other.Description | ||||||
|  |             && Inputs.SequenceEqual(other.Inputs); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override bool Equals(object? obj) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, obj)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, obj)) | ||||||
|  |             return true; | ||||||
|  |         if (obj.GetType() != GetType()) | ||||||
|  |             return false; | ||||||
|  |  | ||||||
|  |         return Equals((CommandSymbol)obj); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override int GetHashCode() => HashCode.Combine(Type, Name, Description, Inputs); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class CommandSymbol | ||||||
|  | { | ||||||
|  |     public static CommandSymbol FromSymbol(INamedTypeSymbol symbol, AttributeData attribute) | ||||||
|  |     { | ||||||
|  |         var inputs = new List<CommandInputSymbol>(); | ||||||
|  |         foreach (var property in symbol.GetMembers().OfType<IPropertySymbol>()) | ||||||
|  |         { | ||||||
|  |             var parameterAttribute = property | ||||||
|  |                 .GetAttributes() | ||||||
|  |                 .FirstOrDefault(a => | ||||||
|  |                     a.AttributeClass?.Name == KnownSymbolNames.CliFxCommandParameterAttribute | ||||||
|  |                 ); | ||||||
|  |  | ||||||
|  |             if (parameterAttribute is not null) | ||||||
|  |             { | ||||||
|  |                 inputs.Add(CommandParameterSymbol.FromSymbol(property, parameterAttribute)); | ||||||
|  |                 continue; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             var optionAttribute = property | ||||||
|  |                 .GetAttributes() | ||||||
|  |                 .FirstOrDefault(a => | ||||||
|  |                     a.AttributeClass?.Name == KnownSymbolNames.CliFxCommandOptionAttribute | ||||||
|  |                 ); | ||||||
|  |  | ||||||
|  |             if (optionAttribute is not null) | ||||||
|  |             { | ||||||
|  |                 inputs.Add(CommandOptionSymbol.FromSymbol(property, optionAttribute)); | ||||||
|  |                 continue; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return new CommandSymbol( | ||||||
|  |             TypeDescriptor.FromSymbol(symbol), | ||||||
|  |             attribute.ConstructorArguments.FirstOrDefault().Value as string, | ||||||
|  |             attribute.GetNamedArgumentValue("Description", default(string)), | ||||||
|  |             inputs | ||||||
|  |         ); | ||||||
|  |     } | ||||||
|  | } | ||||||
							
								
								
									
										10
									
								
								CliFx.SourceGeneration/SemanticModel/KnownSymbolNames.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								CliFx.SourceGeneration/SemanticModel/KnownSymbolNames.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,10 @@ | |||||||
|  | namespace CliFx.SourceGeneration.SemanticModel; | ||||||
|  |  | ||||||
|  | internal static class KnownSymbolNames | ||||||
|  | { | ||||||
|  |     public const string CliFxCommandInterface = "CliFx.ICommand"; | ||||||
|  |     public const string CliFxCommandAttribute = "CliFx.Attributes.CommandAttribute"; | ||||||
|  |     public const string CliFxCommandParameterAttribute = | ||||||
|  |         "CliFx.Attributes.CommandParameterAttribute"; | ||||||
|  |     public const string CliFxCommandOptionAttribute = "CliFx.Attributes.CommandOptionAttribute"; | ||||||
|  | } | ||||||
							
								
								
									
										44
									
								
								CliFx.SourceGeneration/SemanticModel/PropertyDescriptor.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								CliFx.SourceGeneration/SemanticModel/PropertyDescriptor.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | |||||||
|  | using System; | ||||||
|  | using Microsoft.CodeAnalysis; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.SemanticModel; | ||||||
|  |  | ||||||
|  | internal partial class PropertyDescriptor(TypeDescriptor type, string name) | ||||||
|  | { | ||||||
|  |     public TypeDescriptor Type { get; } = type; | ||||||
|  |  | ||||||
|  |     public string Name { get; } = name; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class PropertyDescriptor : IEquatable<PropertyDescriptor> | ||||||
|  | { | ||||||
|  |     public bool Equals(PropertyDescriptor? other) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, other)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, other)) | ||||||
|  |             return true; | ||||||
|  |  | ||||||
|  |         return Type.Equals(other.Type) && Name == other.Name; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override bool Equals(object? obj) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, obj)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, obj)) | ||||||
|  |             return true; | ||||||
|  |         if (obj.GetType() != GetType()) | ||||||
|  |             return false; | ||||||
|  |  | ||||||
|  |         return Equals((PropertyDescriptor)obj); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override int GetHashCode() => HashCode.Combine(Type, Name); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class PropertyDescriptor | ||||||
|  | { | ||||||
|  |     public static PropertyDescriptor FromSymbol(IPropertySymbol symbol) => | ||||||
|  |         new(TypeDescriptor.FromSymbol(symbol.Type), symbol.Name); | ||||||
|  | } | ||||||
							
								
								
									
										47
									
								
								CliFx.SourceGeneration/SemanticModel/TypeDescriptor.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								CliFx.SourceGeneration/SemanticModel/TypeDescriptor.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,47 @@ | |||||||
|  | using System; | ||||||
|  | using CliFx.SourceGeneration.Utils.Extensions; | ||||||
|  | using Microsoft.CodeAnalysis; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.SemanticModel; | ||||||
|  |  | ||||||
|  | internal partial class TypeDescriptor(string fullyQualifiedName) | ||||||
|  | { | ||||||
|  |     public string FullyQualifiedName { get; } = fullyQualifiedName; | ||||||
|  |  | ||||||
|  |     public string Namespace { get; } = fullyQualifiedName.SubstringUntilLast("."); | ||||||
|  |  | ||||||
|  |     public string Name { get; } = fullyQualifiedName.SubstringAfterLast("."); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class TypeDescriptor : IEquatable<TypeDescriptor> | ||||||
|  | { | ||||||
|  |     public bool Equals(TypeDescriptor? other) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, other)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, other)) | ||||||
|  |             return true; | ||||||
|  |  | ||||||
|  |         return FullyQualifiedName == other.FullyQualifiedName; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override bool Equals(object? obj) | ||||||
|  |     { | ||||||
|  |         if (ReferenceEquals(null, obj)) | ||||||
|  |             return false; | ||||||
|  |         if (ReferenceEquals(this, obj)) | ||||||
|  |             return true; | ||||||
|  |         if (obj.GetType() != GetType()) | ||||||
|  |             return false; | ||||||
|  |  | ||||||
|  |         return Equals((TypeDescriptor)obj); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public override int GetHashCode() => FullyQualifiedName.GetHashCode(); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | internal partial class TypeDescriptor | ||||||
|  | { | ||||||
|  |     public static TypeDescriptor FromSymbol(ITypeSymbol symbol) => | ||||||
|  |         new(symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)); | ||||||
|  | } | ||||||
| @@ -0,0 +1,16 @@ | |||||||
|  | using System.Collections.Generic; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.Utils.Extensions; | ||||||
|  |  | ||||||
|  | internal static class CollectionExtensions | ||||||
|  | { | ||||||
|  |     public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source) | ||||||
|  |         where T : class | ||||||
|  |     { | ||||||
|  |         foreach (var i in source) | ||||||
|  |         { | ||||||
|  |             if (i is not null) | ||||||
|  |                 yield return i; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -0,0 +1,9 @@ | |||||||
|  | using System; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.Utils.Extensions; | ||||||
|  |  | ||||||
|  | internal static class GenericExtensions | ||||||
|  | { | ||||||
|  |     public static TOut Pipe<TIn, TOut>(this TIn input, Func<TIn, TOut> transform) => | ||||||
|  |         transform(input); | ||||||
|  | } | ||||||
							
								
								
									
										39
									
								
								CliFx.SourceGeneration/Utils/Extensions/RoslynExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								CliFx.SourceGeneration/Utils/Extensions/RoslynExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,39 @@ | |||||||
|  | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Collections.Immutable; | ||||||
|  | using System.Linq; | ||||||
|  | using Microsoft.CodeAnalysis; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.Utils.Extensions; | ||||||
|  |  | ||||||
|  | internal static class RoslynExtensions | ||||||
|  | { | ||||||
|  |     public static bool DisplayNameMatches(this ISymbol symbol, string name) => | ||||||
|  |         string.Equals( | ||||||
|  |             // Fully qualified name, without `global::` | ||||||
|  |             symbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat), | ||||||
|  |             name, | ||||||
|  |             StringComparison.Ordinal | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |     public static T GetNamedArgumentValue<T>( | ||||||
|  |         this AttributeData attribute, | ||||||
|  |         string name, | ||||||
|  |         T defaultValue = default | ||||||
|  |     ) => | ||||||
|  |         attribute.NamedArguments.FirstOrDefault(i => i.Key == name).Value.Value is T valueAsT | ||||||
|  |             ? valueAsT | ||||||
|  |             : defaultValue; | ||||||
|  |  | ||||||
|  |     public static IReadOnlyList<T> GetNamedArgumentValues<T>( | ||||||
|  |         this AttributeData attribute, | ||||||
|  |         string name | ||||||
|  |     ) | ||||||
|  |         where T : class => | ||||||
|  |         attribute.NamedArguments.FirstOrDefault(i => i.Key == name).Value.Values.CastArray<T>(); | ||||||
|  |  | ||||||
|  |     public static IncrementalValuesProvider<T> WhereNotNull<T>( | ||||||
|  |         this IncrementalValuesProvider<T?> values | ||||||
|  |     ) | ||||||
|  |         where T : class => values.Where(i => i is not null); | ||||||
|  | } | ||||||
							
								
								
									
										30
									
								
								CliFx.SourceGeneration/Utils/Extensions/StringExtensions.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								CliFx.SourceGeneration/Utils/Extensions/StringExtensions.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | |||||||
|  | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
|  |  | ||||||
|  | namespace CliFx.SourceGeneration.Utils.Extensions; | ||||||
|  |  | ||||||
|  | internal static class StringExtensions | ||||||
|  | { | ||||||
|  |     public static string SubstringUntilLast( | ||||||
|  |         this string str, | ||||||
|  |         string sub, | ||||||
|  |         StringComparison comparison = StringComparison.Ordinal | ||||||
|  |     ) | ||||||
|  |     { | ||||||
|  |         var index = str.LastIndexOf(sub, comparison); | ||||||
|  |         return index < 0 ? str : str[..index]; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static string SubstringAfterLast( | ||||||
|  |         this string str, | ||||||
|  |         string sub, | ||||||
|  |         StringComparison comparison = StringComparison.Ordinal | ||||||
|  |     ) | ||||||
|  |     { | ||||||
|  |         var index = str.LastIndexOf(sub, comparison); | ||||||
|  |         return index >= 0 ? str.Substring(index + sub.Length, str.Length - index - sub.Length) : ""; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public static string JoinToString<T>(this IEnumerable<T> source, string separator) => | ||||||
|  |         string.Join(separator, source); | ||||||
|  | } | ||||||
| @@ -1,14 +1,18 @@ | |||||||
| <Project Sdk="Microsoft.NET.Sdk"> | <Project Sdk="Microsoft.NET.Sdk"> | ||||||
|   <Import Project="../CliFx.props" /> |  | ||||||
|  |  | ||||||
|   <PropertyGroup> |   <PropertyGroup> | ||||||
|     <OutputType>Exe</OutputType> |     <OutputType>Exe</OutputType> | ||||||
|     <TargetFramework>netcoreapp3.1</TargetFramework> |     <TargetFramework>net9.0</TargetFramework> | ||||||
|  |     <ApplicationIcon>../favicon.ico</ApplicationIcon> | ||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
|  |  | ||||||
|  |   <ItemGroup> | ||||||
|  |     <PackageReference Include="CSharpier.MsBuild" Version="1.1.2" PrivateAssets="all" /> | ||||||
|  |   </ItemGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <ProjectReference Include="..\CliFx\CliFx.csproj" /> |     <ProjectReference Include="..\CliFx\CliFx.csproj" /> | ||||||
|     <ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" /> |     <ProjectReference Include="..\CliFx.SourceGeneration\CliFx.SourceGeneration.csproj" ReferenceOutputAssembly="false" OutputItemType="analyzer" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|  |  | ||||||
| </Project> | </Project> | ||||||
							
								
								
									
										27
									
								
								CliFx.Tests.Dummy/Commands/CancellationTestCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								CliFx.Tests.Dummy/Commands/CancellationTestCommand.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | |||||||
|  | using System; | ||||||
|  | using System.Threading.Tasks; | ||||||
|  | using CliFx.Attributes; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  |  | ||||||
|  | namespace CliFx.Tests.Dummy.Commands; | ||||||
|  |  | ||||||
|  | [Command("cancel-test")] | ||||||
|  | public class CancellationTestCommand : ICommand | ||||||
|  | { | ||||||
|  |     public async ValueTask ExecuteAsync(IConsole console) | ||||||
|  |     { | ||||||
|  |         try | ||||||
|  |         { | ||||||
|  |             console.WriteLine("Started."); | ||||||
|  |  | ||||||
|  |             await Task.Delay(TimeSpan.FromSeconds(3), console.RegisterCancellationHandler()); | ||||||
|  |  | ||||||
|  |             console.WriteLine("Completed."); | ||||||
|  |         } | ||||||
|  |         catch (OperationCanceledException) | ||||||
|  |         { | ||||||
|  |             console.WriteLine("Cancelled."); | ||||||
|  |             throw; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,23 +1,23 @@ | |||||||
| using System; | using System; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| using CliFx.Attributes; | using CliFx.Attributes; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  |  | ||||||
| namespace CliFx.Tests.Dummy.Commands | namespace CliFx.Tests.Dummy.Commands; | ||||||
|  |  | ||||||
|  | [Command("console-test")] | ||||||
|  | public class ConsoleTestCommand : ICommand | ||||||
| { | { | ||||||
|     [Command("console-test")] |  | ||||||
|     public class ConsoleTestCommand : ICommand |  | ||||||
|     { |  | ||||||
|     public ValueTask ExecuteAsync(IConsole console) |     public ValueTask ExecuteAsync(IConsole console) | ||||||
|     { |     { | ||||||
|         var input = console.Input.ReadToEnd(); |         var input = console.Input.ReadToEnd(); | ||||||
|  |  | ||||||
|             console.WithColors(ConsoleColor.Black, ConsoleColor.White, () => |         using (console.WithColors(ConsoleColor.Black, ConsoleColor.White)) | ||||||
|         { |         { | ||||||
|             console.Output.WriteLine(input); |             console.Output.WriteLine(input); | ||||||
|             console.Error.WriteLine(input); |             console.Error.WriteLine(input); | ||||||
|             }); |         } | ||||||
|  |  | ||||||
|         return default; |         return default; | ||||||
|     } |     } | ||||||
|     } |  | ||||||
| } | } | ||||||
							
								
								
									
										18
									
								
								CliFx.Tests.Dummy/Commands/EnvironmentTestCommand.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								CliFx.Tests.Dummy/Commands/EnvironmentTestCommand.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | using System.Threading.Tasks; | ||||||
|  | using CliFx.Attributes; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  |  | ||||||
|  | namespace CliFx.Tests.Dummy.Commands; | ||||||
|  |  | ||||||
|  | [Command("env-test")] | ||||||
|  | public class EnvironmentTestCommand : ICommand | ||||||
|  | { | ||||||
|  |     [CommandOption("target", EnvironmentVariable = "ENV_TARGET")] | ||||||
|  |     public string GreetingTarget { get; init; } = "World"; | ||||||
|  |  | ||||||
|  |     public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |     { | ||||||
|  |         console.WriteLine($"Hello {GreetingTarget}!"); | ||||||
|  |         return default; | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,19 +0,0 @@ | |||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests.Dummy.Commands |  | ||||||
| { |  | ||||||
|     [Command] |  | ||||||
|     public class HelloWorldCommand : ICommand |  | ||||||
|     { |  | ||||||
|         [CommandOption("target", EnvironmentVariableName = "ENV_TARGET")] |  | ||||||
|         public string Target { get; set; } = "World"; |  | ||||||
|  |  | ||||||
|         public ValueTask ExecuteAsync(IConsole console) |  | ||||||
|         { |  | ||||||
|             console.Output.WriteLine($"Hello {Target}!"); |  | ||||||
|  |  | ||||||
|             return default; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,21 +1,28 @@ | |||||||
| using System.Reflection; | using System; | ||||||
|  | using System.IO; | ||||||
|  | using System.Reflection; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
|  |  | ||||||
| namespace CliFx.Tests.Dummy | namespace CliFx.Tests.Dummy; | ||||||
|  |  | ||||||
|  | // This dummy application is used in tests for scenarios that require an external process to properly verify | ||||||
|  | public static class Program | ||||||
| { | { | ||||||
|     public static partial class Program |     // Path to the apphost | ||||||
|     { |     public static string FilePath { get; } = | ||||||
|         public static Assembly Assembly { get; } = typeof(Program).Assembly; |         Path.ChangeExtension( | ||||||
|  |             Assembly.GetExecutingAssembly().Location, | ||||||
|  |             OperatingSystem.IsWindows() ? "exe" : null | ||||||
|  |         ); | ||||||
|  |  | ||||||
|         public static string Location { get; } = Assembly.Location; |     public static async Task Main() | ||||||
|     } |  | ||||||
|  |  | ||||||
|     public static partial class Program |  | ||||||
|     { |     { | ||||||
|         public static async Task Main() => |         // Make sure color codes are not produced because we rely on the output in tests | ||||||
|             await new CliApplicationBuilder() |         Environment.SetEnvironmentVariable( | ||||||
|                 .AddCommandsFromThisAssembly() |             "DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION", | ||||||
|                 .Build() |             "false" | ||||||
|                 .RunAsync(); |         ); | ||||||
|  |  | ||||||
|  |         await new CliApplicationBuilder().AddCommandsFromThisAssembly().Build().RunAsync(); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -1,179 +0,0 @@ | |||||||
| using System.Collections.Generic; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class ApplicationSpecs |  | ||||||
|     { |  | ||||||
|         [Command] |  | ||||||
|         private class DefaultCommand : ICommand |  | ||||||
|         { |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class AnotherDefaultCommand : ICommand |  | ||||||
|         { |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class NonImplementedCommand |  | ||||||
|         { |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private class NonAnnotatedCommand : ICommand |  | ||||||
|         { |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("dup")] |  | ||||||
|         private class DuplicateNameCommandA : ICommand |  | ||||||
|         { |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("dup")] |  | ||||||
|         private class DuplicateNameCommandB : ICommand |  | ||||||
|         { |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class DuplicateParameterOrderCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandParameter(13)] |  | ||||||
|             public string? ParameterA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandParameter(13)] |  | ||||||
|             public string? ParameterB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class DuplicateParameterNameCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandParameter(0, Name = "param")] |  | ||||||
|             public string? ParameterA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandParameter(1, Name = "param")] |  | ||||||
|             public string? ParameterB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class MultipleNonScalarParametersCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandParameter(0)] |  | ||||||
|             public IReadOnlyList<string>? ParameterA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandParameter(1)] |  | ||||||
|             public IReadOnlyList<string>? ParameterB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class NonLastNonScalarParameterCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandParameter(0)] |  | ||||||
|             public IReadOnlyList<string>? ParameterA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandParameter(1)] |  | ||||||
|             public string? ParameterB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class EmptyOptionNameCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("")] |  | ||||||
|             public string? Apples { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class SingleCharacterOptionNameCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("a")] |  | ||||||
|             public string? Apples { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class DuplicateOptionNamesCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("fruits")] |  | ||||||
|             public string? Apples { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("fruits")] |  | ||||||
|             public string? Oranges { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class DuplicateOptionShortNamesCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption('x')] |  | ||||||
|             public string? OptionA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption('x')] |  | ||||||
|             public string? OptionB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class ConflictWithHelpOptionCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("option-h", 'h')] |  | ||||||
|             public string? OptionH { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class ConflictWithVersionOptionCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("version")] |  | ||||||
|             public string? Version { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class DuplicateOptionEnvironmentVariableNamesCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")] |  | ||||||
|             public string? OptionA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")] |  | ||||||
|             public string? OptionB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("hidden", Description = "Description")] |  | ||||||
|         private class HiddenPropertiesCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandParameter(13, Name = "param", Description = "Param description")] |  | ||||||
|             public string? Parameter { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option", 'o', Description = "Option description", EnvironmentVariableName = "ENV")] |  | ||||||
|             public string? Option { get; set; } |  | ||||||
|  |  | ||||||
|             public string? HiddenA { get; set; } |  | ||||||
|  |  | ||||||
|             public bool? HiddenB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,269 +1,71 @@ | |||||||
| using System; | using System; | ||||||
| using System.IO; | using System.Collections.Generic; | ||||||
| using CliFx.Domain; | using System.Threading.Tasks; | ||||||
| using CliFx.Exceptions; | using CliFx.Tests.Utils; | ||||||
| using FluentAssertions; | using FluentAssertions; | ||||||
| using Xunit; | using Xunit; | ||||||
| using Xunit.Abstractions; | using Xunit.Abstractions; | ||||||
|  |  | ||||||
| namespace CliFx.Tests | namespace CliFx.Tests; | ||||||
|  |  | ||||||
|  | public class ApplicationSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) | ||||||
| { | { | ||||||
|     public partial class ApplicationSpecs |  | ||||||
|     { |  | ||||||
|         private readonly ITestOutputHelper _output; |  | ||||||
|  |  | ||||||
|         public ApplicationSpecs(ITestOutputHelper output) => _output = output; |  | ||||||
|  |  | ||||||
|     [Fact] |     [Fact] | ||||||
|         public void Application_can_be_created_with_a_default_configuration() |     public async Task I_can_create_an_application_with_the_default_configuration() | ||||||
|     { |     { | ||||||
|         // Act |         // Act | ||||||
|         var app = new CliApplicationBuilder() |         var app = new CliApplicationBuilder() | ||||||
|             .AddCommandsFromThisAssembly() |             .AddCommandsFromThisAssembly() | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|             .Build(); |             .Build(); | ||||||
|  |  | ||||||
|  |         var exitCode = await app.RunAsync([], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|         // Assert |         // Assert | ||||||
|             app.Should().NotBeNull(); |         exitCode.Should().Be(0); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     [Fact] |     [Fact] | ||||||
|         public void Application_can_be_created_with_a_custom_configuration() |     public async Task I_can_create_an_application_with_a_custom_configuration() | ||||||
|     { |     { | ||||||
|         // Act |         // Act | ||||||
|         var app = new CliApplicationBuilder() |         var app = new CliApplicationBuilder() | ||||||
|                 .AddCommand(typeof(DefaultCommand)) |             .AddCommand<NoOpCommand>() | ||||||
|                 .AddCommandsFrom(typeof(DefaultCommand).Assembly) |             .AddCommandsFrom(typeof(NoOpCommand).Assembly) | ||||||
|                 .AddCommands(new[] {typeof(DefaultCommand)}) |             .AddCommands([typeof(NoOpCommand)]) | ||||||
|                 .AddCommandsFrom(new[] {typeof(DefaultCommand).Assembly}) |             .AddCommandsFrom([typeof(NoOpCommand).Assembly]) | ||||||
|             .AddCommandsFromThisAssembly() |             .AddCommandsFromThisAssembly() | ||||||
|             .AllowDebugMode() |             .AllowDebugMode() | ||||||
|             .AllowPreviewMode() |             .AllowPreviewMode() | ||||||
|                 .UseTitle("test") |             .SetTitle("test") | ||||||
|                 .UseExecutableName("test") |             .SetExecutableName("test") | ||||||
|                 .UseVersionText("test") |             .SetVersion("test") | ||||||
|                 .UseDescription("test") |             .SetDescription("test") | ||||||
|                 .UseConsole(new VirtualConsole(Stream.Null)) |             .UseConsole(FakeConsole) | ||||||
|             .UseTypeActivator(Activator.CreateInstance!) |             .UseTypeActivator(Activator.CreateInstance!) | ||||||
|             .Build(); |             .Build(); | ||||||
|  |  | ||||||
|  |         var exitCode = await app.RunAsync([], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|         // Assert |         // Assert | ||||||
|             app.Should().NotBeNull(); |         exitCode.Should().Be(0); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     [Fact] |     [Fact] | ||||||
|         public void At_least_one_command_must_be_defined_in_an_application() |     public async Task I_can_try_to_create_an_application_and_get_an_error_if_it_has_invalid_commands() | ||||||
|     { |     { | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = Array.Empty<Type>(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Commands_must_implement_the_corresponding_interface() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(NonImplementedCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Commands_must_be_annotated_by_an_attribute() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(NonAnnotatedCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Commands_must_have_unique_names() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_can_be_default_but_only_if_it_is_the_only_such_command() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(DefaultCommand), typeof(AnotherDefaultCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_parameters_must_have_unique_order() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_parameters_must_have_unique_names() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(DuplicateParameterNameCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_parameter_can_be_non_scalar_only_if_no_other_such_parameter_is_present() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_parameter_can_be_non_scalar_only_if_it_is_the_last_in_order() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_options_must_have_names_that_are_not_empty() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(EmptyOptionNameCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_options_must_have_names_that_are_longer_than_one_character() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(SingleCharacterOptionNameCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_options_must_have_unique_names() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_options_must_have_unique_short_names() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_options_must_not_have_conflicts_with_the_implicit_help_option() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(ConflictWithHelpOptionCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_options_must_not_have_conflicts_with_the_implicit_version_option() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(ConflictWithVersionOptionCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_options_must_have_unique_environment_variable_names() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)}; |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Command_options_and_parameters_must_be_annotated_by_corresponding_attributes() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandTypes = new[] {typeof(HiddenPropertiesCommand)}; |  | ||||||
|  |  | ||||||
|         // Act |         // Act | ||||||
|             var schema = RootSchema.Resolve(commandTypes); |         var app = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(typeof(ApplicationSpecs)) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         var exitCode = await app.RunAsync([], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|         // Assert |         // Assert | ||||||
|             schema.Should().BeEquivalentTo(new RootSchema(new[] |         exitCode.Should().NotBe(0); | ||||||
|             { |  | ||||||
|                 new CommandSchema( |  | ||||||
|                     typeof(HiddenPropertiesCommand), |  | ||||||
|                     "hidden", |  | ||||||
|                     "Description", |  | ||||||
|                     new[] |  | ||||||
|                     { |  | ||||||
|                         new CommandParameterSchema( |  | ||||||
|                             typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter))!, |  | ||||||
|                             13, |  | ||||||
|                             "param", |  | ||||||
|                             "Param description") |  | ||||||
|                     }, |  | ||||||
|                     new[] |  | ||||||
|                     { |  | ||||||
|                         new CommandOptionSchema( |  | ||||||
|                             typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option))!, |  | ||||||
|                             "option", |  | ||||||
|                             'o', |  | ||||||
|                             "ENV", |  | ||||||
|                             false, |  | ||||||
|                             "Option description"), |  | ||||||
|                         CommandOptionSchema.HelpOption |  | ||||||
|                     }) |  | ||||||
|             })); |  | ||||||
|  |  | ||||||
|             schema.ToString().Should().NotBeNullOrWhiteSpace(); // this is only for coverage, I'm sorry |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|         } |         stdErr.Should().Contain("not a valid command"); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -1,191 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Collections; |  | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class ArgumentBindingSpecs |  | ||||||
|     { |  | ||||||
|         [Command] |  | ||||||
|         private class AllSupportedTypesCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption(nameof(Object))] |  | ||||||
|             public object? Object { get; set; } = 42; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(String))] |  | ||||||
|             public string? String { get; set; } = "foo bar"; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Bool))] |  | ||||||
|             public bool Bool { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Char))] |  | ||||||
|             public char Char { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Sbyte))] |  | ||||||
|             public sbyte Sbyte { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Byte))] |  | ||||||
|             public byte Byte { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Short))] |  | ||||||
|             public short Short { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Ushort))] |  | ||||||
|             public ushort Ushort { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Int))] |  | ||||||
|             public int Int { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Uint))] |  | ||||||
|             public uint Uint { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Long))] |  | ||||||
|             public long Long { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Ulong))] |  | ||||||
|             public ulong Ulong { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Float))] |  | ||||||
|             public float Float { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Double))] |  | ||||||
|             public double Double { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Decimal))] |  | ||||||
|             public decimal Decimal { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(DateTime))] |  | ||||||
|             public DateTime DateTime { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(DateTimeOffset))] |  | ||||||
|             public DateTimeOffset DateTimeOffset { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(TimeSpan))] |  | ||||||
|             public TimeSpan TimeSpan { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(CustomEnum))] |  | ||||||
|             public CustomEnum CustomEnum { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(IntNullable))] |  | ||||||
|             public int? IntNullable { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(CustomEnumNullable))] |  | ||||||
|             public CustomEnum? CustomEnumNullable { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(TimeSpanNullable))] |  | ||||||
|             public TimeSpan? TimeSpanNullable { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(TestStringConstructable))] |  | ||||||
|             public StringConstructable? TestStringConstructable { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(TestStringParseable))] |  | ||||||
|             public StringParseable? TestStringParseable { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(TestStringParseableWithFormatProvider))] |  | ||||||
|             public StringParseableWithFormatProvider? TestStringParseableWithFormatProvider { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(ObjectArray))] |  | ||||||
|             public object[]? ObjectArray { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(StringArray))] |  | ||||||
|             public string[]? StringArray { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(IntArray))] |  | ||||||
|             public int[]? IntArray { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(CustomEnumArray))] |  | ||||||
|             public CustomEnum[]? CustomEnumArray { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(IntNullableArray))] |  | ||||||
|             public int?[]? IntNullableArray { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(TestStringConstructableArray))] |  | ||||||
|             public StringConstructable[]? TestStringConstructableArray { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Enumerable))] |  | ||||||
|             public IEnumerable? Enumerable { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(StringEnumerable))] |  | ||||||
|             public IEnumerable<string>? StringEnumerable { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(StringReadOnlyList))] |  | ||||||
|             public IReadOnlyList<string>? StringReadOnlyList { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(StringList))] |  | ||||||
|             public List<string>? StringList { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(StringHashSet))] |  | ||||||
|             public HashSet<string>? StringHashSet { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class ArrayOptionCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("option", 'o')] |  | ||||||
|             public IReadOnlyList<string>? Option { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class RequiredOptionCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption(nameof(OptionA))] |  | ||||||
|             public string? OptionA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(OptionB), IsRequired = true)] |  | ||||||
|             public string? OptionB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class ParametersCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandParameter(0)] |  | ||||||
|             public string? ParameterA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandParameter(1)] |  | ||||||
|             public string? ParameterB { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandParameter(2)] |  | ||||||
|             public IReadOnlyList<string>? ParameterC { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class UnsupportedPropertyTypeCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption(nameof(Option))] |  | ||||||
|             public DummyType? Option { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class UnsupportedEnumerablePropertyTypeCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption(nameof(Option))] |  | ||||||
|             public CustomEnumerable<string>? Option { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class NoParameterCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption(nameof(OptionA))] |  | ||||||
|             public string? OptionA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(OptionB))] |  | ||||||
|             public string? OptionB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,53 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Collections; |  | ||||||
| using System.Collections.Generic; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class ArgumentBindingSpecs |  | ||||||
|     { |  | ||||||
|         private enum CustomEnum |  | ||||||
|         { |  | ||||||
|             Value1 = 1, |  | ||||||
|             Value2 = 2, |  | ||||||
|             Value3 = 3 |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private class StringConstructable |  | ||||||
|         { |  | ||||||
|             public string Value { get; } |  | ||||||
|  |  | ||||||
|             public StringConstructable(string value) => Value = value; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private class StringParseable |  | ||||||
|         { |  | ||||||
|             public string Value { get; } |  | ||||||
|  |  | ||||||
|             private StringParseable(string value) => Value = value; |  | ||||||
|  |  | ||||||
|             public static StringParseable Parse(string value) => new StringParseable(value); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private class StringParseableWithFormatProvider |  | ||||||
|         { |  | ||||||
|             public string Value { get; } |  | ||||||
|  |  | ||||||
|             private StringParseableWithFormatProvider(string value) => Value = value; |  | ||||||
|  |  | ||||||
|             public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) => |  | ||||||
|                 new StringParseableWithFormatProvider(value + " " + formatProvider); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private class DummyType |  | ||||||
|         { |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public class CustomEnumerable<T> : IEnumerable<T> |  | ||||||
|         { |  | ||||||
|             public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator(); |  | ||||||
|  |  | ||||||
|             IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,992 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Globalization; |  | ||||||
| using CliFx.Exceptions; |  | ||||||
| using CliFx.Tests.Internal; |  | ||||||
| using FluentAssertions; |  | ||||||
| using Xunit; |  | ||||||
| using Xunit.Abstractions; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class ArgumentBindingSpecs |  | ||||||
|     { |  | ||||||
|         private readonly ITestOutputHelper _output; |  | ||||||
|  |  | ||||||
|         public ArgumentBindingSpecs(ITestOutputHelper output) => _output = output; |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_object_is_bound_directly_from_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Object), "value") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Object = "value" |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_object_array_is_bound_directly_from_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.ObjectArray), "foo", "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 ObjectArray = new object[] {"foo", "bar"} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_non_generic_IEnumerable_is_bound_directly_from_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Enumerable), "foo", "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Enumerable = new object[] {"foo", "bar"} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_string_is_bound_directly_from_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.String), "value") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 String = "value" |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_string_array_is_bound_directly_from_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.StringArray), "foo", "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 StringArray = new[] {"foo", "bar"} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_string_IEnumerable_is_bound_directly_from_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.StringEnumerable), "foo", "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 StringEnumerable = new[] {"foo", "bar"} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_string_IReadOnlyList_is_bound_directly_from_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.StringReadOnlyList), "foo", "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 StringReadOnlyList = new[] {"foo", "bar"} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_string_List_is_bound_directly_from_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.StringList), "foo", "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 StringList = new List<string> {"foo", "bar"} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_string_HashSet_is_bound_directly_from_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.StringHashSet), "foo", "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 StringHashSet = new HashSet<string>(new[] {"foo", "bar"}) |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_bool_is_bound_as_true_if_the_argument_value_is_true() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Bool), "true") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Bool = true |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_bool_is_bound_as_false_if_the_argument_value_is_false() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Bool), "false") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Bool = false |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_bool_is_bound_as_true_if_the_argument_value_is_not_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Bool)) |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Bool = true |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_char_is_bound_directly_from_the_argument_value_if_it_contains_only_one_character() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Char), "a") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Char = 'a' |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_sbyte_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Sbyte), "15") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Sbyte = 15 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_byte_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Byte), "15") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Byte = 15 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_short_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Short), "15") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Short = 15 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_ushort_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Ushort), "15") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Ushort = 15 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_int_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Int), "15") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Int = 15 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_nullable_int_is_bound_by_parsing_the_argument_value_if_it_is_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.IntNullable), "15") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 IntNullable = 15 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_nullable_int_is_bound_as_null_if_the_argument_value_is_not_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.IntNullable)) |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 IntNullable = null |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_int_array_is_bound_by_parsing_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.IntArray), "3", "14") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 IntArray = new[] {3, 14} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_nullable_int_array_is_bound_by_parsing_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.IntNullableArray), "3", "14") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 IntNullableArray = new int?[] {3, 14} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_uint_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Uint), "15") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Uint = 15 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_long_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Long), "15") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Long = 15 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_ulong_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Ulong), "15") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Ulong = 15 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_float_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Float), "123.45") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Float = 123.45F |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_double_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Double), "123.45") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Double = 123.45 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_decimal_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Decimal), "123.45") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 Decimal = 123.45M |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_DateTime_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.DateTime), "28 Apr 1995") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 DateTime = new DateTime(1995, 04, 28) |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_DateTimeOffset_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.DateTimeOffset), "28 Apr 1995") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 DateTimeOffset = new DateTime(1995, 04, 28) |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_TimeSpan_is_bound_by_parsing_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.TimeSpan), "00:14:59") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 TimeSpan = new TimeSpan(00, 14, 59) |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_nullable_TimeSpan_is_bound_by_parsing_the_argument_value_if_it_is_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.TimeSpanNullable), "00:14:59") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 TimeSpanNullable = new TimeSpan(00, 14, 59) |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_type_nullable_TimeSpan_is_bound_as_null_if_the_argument_value_is_not_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.TimeSpanNullable)) |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 TimeSpanNullable = null |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_an_enum_type_is_bound_by_parsing_the_argument_value_as_name() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.CustomEnum), "value2") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 CustomEnum = CustomEnum.Value2 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_an_enum_type_is_bound_by_parsing_the_argument_value_as_id() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.CustomEnum), "2") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 CustomEnum = CustomEnum.Value2 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_a_nullable_enum_type_is_bound_by_parsing_the_argument_value_as_name_if_it_is_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.CustomEnumNullable), "value3") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 CustomEnumNullable = CustomEnum.Value3 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_a_nullable_enum_type_is_bound_by_parsing_the_argument_value_as_id_if_it_is_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.CustomEnumNullable), "3") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 CustomEnumNullable = CustomEnum.Value3 |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_a_nullable_enum_type_is_bound_as_null_if_the_argument_value_is_not_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.CustomEnumNullable)) |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 CustomEnumNullable = null |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_an_enum_array_type_is_bound_by_parsing_the_argument_values_as_names() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.CustomEnumArray), "value1", "value3") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 CustomEnumArray = new[] {CustomEnum.Value1, CustomEnum.Value3} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_an_enum_array_type_is_bound_by_parsing_the_argument_values_as_ids() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.CustomEnumArray), "1", "3") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 CustomEnumArray = new[] {CustomEnum.Value1, CustomEnum.Value3} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_an_enum_array_type_is_bound_by_parsing_the_argument_values_as_either_names_or_ids() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.CustomEnumArray), "value1", "3") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 CustomEnumArray = new[] {CustomEnum.Value1, CustomEnum.Value3} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_a_type_that_has_a_constructor_accepting_a_string_is_bound_by_invoking_the_constructor_with_the_argument_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.TestStringConstructable), "foobar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 TestStringConstructable = new StringConstructable("foobar") |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_an_array_of_type_that_has_a_constructor_accepting_a_string_is_bound_by_invoking_the_constructor_with_the_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.TestStringConstructableArray), "foo", "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 TestStringConstructableArray = new[] {new StringConstructable("foo"), new StringConstructable("bar") } |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_a_type_that_has_a_static_Parse_method_accepting_a_string_is_bound_by_invoking_the_method() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.TestStringParseable), "foobar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 TestStringParseable = StringParseable.Parse("foobar") |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_a_type_that_has_a_static_Parse_method_accepting_a_string_and_format_provider_is_bound_by_invoking_the_method() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.TestStringParseableWithFormatProvider), "foobar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new AllSupportedTypesCommand |  | ||||||
|             { |  | ||||||
|                 TestStringParseableWithFormatProvider = StringParseableWithFormatProvider.Parse("foobar", CultureInfo.InvariantCulture) |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_annotated_as_an_option_can_be_bound_from_multiple_values_even_if_the_inputs_use_mixed_naming() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption("option", "foo") |  | ||||||
|                 .AddOption("o", "bar") |  | ||||||
|                 .AddOption("option", "baz") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<ArrayOptionCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new ArrayOptionCommand |  | ||||||
|             { |  | ||||||
|                 Option = new[] {"foo", "bar", "baz"} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_annotated_as_a_required_option_must_always_be_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(RequiredOptionCommand.OptionA), "foo") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => CommandHelper.ResolveCommand<RequiredOptionCommand>(input)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_annotated_as_a_required_option_must_always_be_bound_to_some_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(RequiredOptionCommand.OptionB)) |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => CommandHelper.ResolveCommand<RequiredOptionCommand>(input)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_annotated_as_parameter_is_bound_directly_from_argument_value_according_to_the_order() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddParameter("foo") |  | ||||||
|                 .AddParameter("bar") |  | ||||||
|                 .AddParameter("hello") |  | ||||||
|                 .AddParameter("world") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<ParametersCommand>(input); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new ParametersCommand |  | ||||||
|             { |  | ||||||
|                 ParameterA = "foo", |  | ||||||
|                 ParameterB = "bar", |  | ||||||
|                 ParameterC = new[] {"hello", "world"} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_annotated_as_parameter_must_always_be_bound_to_some_value() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddParameter("foo") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => CommandHelper.ResolveCommand<ParametersCommand>(input)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_custom_type_that_implements_IEnumerable_can_only_be_bound_if_that_type_has_a_constructor_accepting_an_array() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(UnsupportedEnumerablePropertyTypeCommand.Option), "foo", "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => CommandHelper.ResolveCommand<UnsupportedEnumerablePropertyTypeCommand>(input)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_of_non_nullable_type_can_only_be_bound_if_the_argument_value_is_set() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Int)) |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_must_have_a_type_supported_by_the_framework_in_order_to_be_bound() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(UnsupportedPropertyTypeCommand.Option), "foo") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => CommandHelper.ResolveCommand<UnsupportedPropertyTypeCommand>(input)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Property_must_have_a_type_that_implements_IEnumerable_in_order_to_be_bound_from_multiple_argument_values() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption(nameof(AllSupportedTypesCommand.Int), "1", "2", "3") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void All_provided_option_arguments_must_be_bound_to_corresponding_properties() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddOption("not-a-real-option", "boom") |  | ||||||
|                 .AddOption("fake-option", "poof") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => CommandHelper.ResolveCommand<AllSupportedTypesCommand>(input)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void All_provided_parameter_arguments_must_be_bound_to_corresponding_properties() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = new CommandInputBuilder() |  | ||||||
|                 .AddParameter("boom") |  | ||||||
|                 .AddParameter("poof") |  | ||||||
|                 .AddOption(nameof(NoParameterCommand.OptionA), "foo") |  | ||||||
|                 .AddOption(nameof(NoParameterCommand.OptionB), "bar") |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => CommandHelper.ResolveCommand<NoParameterCommand>(input)); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,378 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Collections.Generic; |  | ||||||
| using CliFx.Domain; |  | ||||||
| using CliFx.Tests.Internal; |  | ||||||
| using FluentAssertions; |  | ||||||
| using Xunit; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public class ArgumentSyntaxSpecs |  | ||||||
|     { |  | ||||||
|         [Fact] |  | ||||||
|         public void Input_is_empty_if_no_arguments_are_provided() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var arguments = Array.Empty<string>(); |  | ||||||
|             var commandNames = Array.Empty<string>(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var input = CommandInput.Parse(arguments, commandNames); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             input.Should().BeEquivalentTo(CommandInput.Empty); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static object[][] DirectivesTestData => new[] |  | ||||||
|         { |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"[preview]"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddDirective("preview") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"[preview]", "[debug]"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddDirective("preview") |  | ||||||
|                     .AddDirective("debug") |  | ||||||
|                     .Build() |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|  |  | ||||||
|         [Theory] |  | ||||||
|         [MemberData(nameof(DirectivesTestData))] |  | ||||||
|         internal void Directive_can_be_enabled_by_specifying_its_name_in_square_brackets(IReadOnlyList<string> arguments, CommandInput expectedInput) |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandNames = Array.Empty<string>(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var input = CommandInput.Parse(arguments, commandNames); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             input.Should().BeEquivalentTo(expectedInput); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static object[][] OptionsTestData => new[] |  | ||||||
|         { |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"--option"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("option") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"--option", "value"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("option", "value") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"--option", "value1", "value2"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("option", "value1", "value2") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"--option", "same value"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("option", "same value") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"--option1", "--option2"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("option1") |  | ||||||
|                     .AddOption("option2") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"--option1", "value1", "--option2", "value2"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("option1", "value1") |  | ||||||
|                     .AddOption("option2", "value2") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"--option1", "value1", "value2", "--option2", "value3", "value4"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("option1", "value1", "value2") |  | ||||||
|                     .AddOption("option2", "value3", "value4") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"--option1", "value1", "value2", "--option2"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("option1", "value1", "value2") |  | ||||||
|                     .AddOption("option2") |  | ||||||
|                     .Build() |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|  |  | ||||||
|         [Theory] |  | ||||||
|         [MemberData(nameof(OptionsTestData))] |  | ||||||
|         internal void Option_can_be_set_by_specifying_its_name_after_two_dashes(IReadOnlyList<string> arguments, CommandInput expectedInput) |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandNames = Array.Empty<string>(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var input = CommandInput.Parse(arguments, commandNames); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             input.Should().BeEquivalentTo(expectedInput); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static object[][] ShortOptionsTestData => new[] |  | ||||||
|         { |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-o"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("o") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-o", "value"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("o", "value") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-o", "value1", "value2"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("o", "value1", "value2") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-o", "same value"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("o", "same value") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-a", "-b"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("a") |  | ||||||
|                     .AddOption("b") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-a", "value1", "-b", "value2"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("a", "value1") |  | ||||||
|                     .AddOption("b", "value2") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-a", "value1", "value2", "-b", "value3", "value4"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("a", "value1", "value2") |  | ||||||
|                     .AddOption("b", "value3", "value4") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-a", "value1", "value2", "-b"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("a", "value1", "value2") |  | ||||||
|                     .AddOption("b") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-abc"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("a") |  | ||||||
|                     .AddOption("b") |  | ||||||
|                     .AddOption("c") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-abc", "value"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("a") |  | ||||||
|                     .AddOption("b") |  | ||||||
|                     .AddOption("c", "value") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"-abc", "value1", "value2"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddOption("a") |  | ||||||
|                     .AddOption("b") |  | ||||||
|                     .AddOption("c", "value1", "value2") |  | ||||||
|                     .Build() |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|  |  | ||||||
|         [Theory] |  | ||||||
|         [MemberData(nameof(ShortOptionsTestData))] |  | ||||||
|         internal void Option_can_be_set_by_specifying_its_short_name_after_a_single_dash(IReadOnlyList<string> arguments, CommandInput expectedInput) |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandNames = Array.Empty<string>(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var input = CommandInput.Parse(arguments, commandNames); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             input.Should().BeEquivalentTo(expectedInput); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static object[][] ParametersTestData => new[] |  | ||||||
|         { |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"foo"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddParameter("foo") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"foo", "bar"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddParameter("foo") |  | ||||||
|                     .AddParameter("bar") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"[preview]", "foo"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddDirective("preview") |  | ||||||
|                     .AddParameter("foo") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"foo", "--option", "value", "-abc"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddParameter("foo") |  | ||||||
|                     .AddOption("option", "value") |  | ||||||
|                     .AddOption("a") |  | ||||||
|                     .AddOption("b") |  | ||||||
|                     .AddOption("c") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"[preview]", "[debug]", "foo", "bar", "--option", "value", "-abc"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .AddDirective("preview") |  | ||||||
|                     .AddDirective("debug") |  | ||||||
|                     .AddParameter("foo") |  | ||||||
|                     .AddParameter("bar") |  | ||||||
|                     .AddOption("option", "value") |  | ||||||
|                     .AddOption("a") |  | ||||||
|                     .AddOption("b") |  | ||||||
|                     .AddOption("c") |  | ||||||
|                     .Build() |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|  |  | ||||||
|         [Theory] |  | ||||||
|         [MemberData(nameof(ParametersTestData))] |  | ||||||
|         internal void Parameter_can_be_set_by_specifying_the_value_directly(IReadOnlyList<string> arguments, CommandInput expectedInput) |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var commandNames = Array.Empty<string>(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var input = CommandInput.Parse(arguments, commandNames); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             input.Should().BeEquivalentTo(expectedInput); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static object[][] CommandNameTestData => new[] |  | ||||||
|         { |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"cmd"}, |  | ||||||
|                 new[] {"cmd"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .SetCommandName("cmd") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"cmd"}, |  | ||||||
|                 new[] {"cmd", "foo", "bar", "-o", "value"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .SetCommandName("cmd") |  | ||||||
|                     .AddParameter("foo") |  | ||||||
|                     .AddParameter("bar") |  | ||||||
|                     .AddOption("o", "value") |  | ||||||
|                     .Build() |  | ||||||
|             }, |  | ||||||
|  |  | ||||||
|             new object[] |  | ||||||
|             { |  | ||||||
|                 new[] {"cmd", "cmd sub"}, |  | ||||||
|                 new[] {"cmd", "sub", "foo"}, |  | ||||||
|                 new CommandInputBuilder() |  | ||||||
|                     .SetCommandName("cmd sub") |  | ||||||
|                     .AddParameter("foo") |  | ||||||
|                     .Build() |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|  |  | ||||||
|         [Theory] |  | ||||||
|         [MemberData(nameof(CommandNameTestData))] |  | ||||||
|         internal void Command_name_is_matched_from_arguments_that_come_before_parameters( |  | ||||||
|             IReadOnlyList<string> commandNames, |  | ||||||
|             IReadOnlyList<string> arguments, |  | ||||||
|             CommandInput expectedInput) |  | ||||||
|         { |  | ||||||
|             // Act |  | ||||||
|             var input = CommandInput.Parse(arguments, commandNames); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             input.Should().BeEquivalentTo(expectedInput); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,27 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class CancellationSpecs |  | ||||||
|     { |  | ||||||
|         [Command("cancel")] |  | ||||||
|         private class CancellableCommand : ICommand |  | ||||||
|         { |  | ||||||
|             public async ValueTask ExecuteAsync(IConsole console) |  | ||||||
|             { |  | ||||||
|                 try |  | ||||||
|                 { |  | ||||||
|                     await Task.Delay(TimeSpan.FromSeconds(3), console.GetCancellationToken()); |  | ||||||
|                     console.Output.WriteLine("Never printed"); |  | ||||||
|                 } |  | ||||||
|                 catch (OperationCanceledException) |  | ||||||
|                 { |  | ||||||
|                     console.Output.WriteLine("Cancellation requested"); |  | ||||||
|                     throw; |  | ||||||
|                 } |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,43 +1,107 @@ | |||||||
| using System; | using System; | ||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| using System.IO; | using System.Text; | ||||||
| using System.Threading; | using System.Threading; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
|  | using CliFx.Tests.Utils; | ||||||
|  | using CliFx.Tests.Utils.Extensions; | ||||||
|  | using CliWrap; | ||||||
| using FluentAssertions; | using FluentAssertions; | ||||||
| using Xunit; | using Xunit; | ||||||
|  | using Xunit.Abstractions; | ||||||
|  |  | ||||||
| namespace CliFx.Tests | namespace CliFx.Tests; | ||||||
|  |  | ||||||
|  | public class CancellationSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) | ||||||
| { | { | ||||||
|     public partial class CancellationSpecs |     [Fact(Timeout = 15000)] | ||||||
|  |     public async Task I_can_configure_the_command_to_listen_to_the_interrupt_signal() | ||||||
|     { |     { | ||||||
|         [Fact] |  | ||||||
|         public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested() |  | ||||||
|         { |  | ||||||
|             // Can't test it with a real console because CliWrap can't send Ctrl+C |  | ||||||
|  |  | ||||||
|         // Arrange |         // Arrange | ||||||
|         using var cts = new CancellationTokenSource(); |         using var cts = new CancellationTokenSource(); | ||||||
|  |  | ||||||
|             await using var stdOut = new MemoryStream(); |         // We need to send the cancellation request right after the process has registered | ||||||
|             var console = new VirtualConsole(output: stdOut, cancellationToken: cts.Token); |         // a handler for the interrupt signal, otherwise the default handler will trigger | ||||||
|  |         // and just kill the process. | ||||||
|  |         void HandleStdOut(string line) | ||||||
|  |         { | ||||||
|  |             if (string.Equals(line, "Started.", StringComparison.OrdinalIgnoreCase)) | ||||||
|  |                 cts.CancelAfter(TimeSpan.FromSeconds(0.2)); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         var stdOutBuffer = new StringBuilder(); | ||||||
|  |  | ||||||
|  |         var pipeTarget = PipeTarget.Merge( | ||||||
|  |             PipeTarget.ToDelegate(HandleStdOut), | ||||||
|  |             PipeTarget.ToStringBuilder(stdOutBuffer) | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var command = Cli.Wrap(Dummy.Program.FilePath).WithArguments("cancel-test") | pipeTarget; | ||||||
|  |  | ||||||
|  |         // Act & assert | ||||||
|  |         await Assert.ThrowsAnyAsync<OperationCanceledException>( | ||||||
|  |             async () => | ||||||
|  |                 await command.ExecuteAsync( | ||||||
|  |                     // Forceful cancellation (not required because we have a timeout) | ||||||
|  |                     CancellationToken.None, | ||||||
|  |                     // Graceful cancellation | ||||||
|  |                     cts.Token | ||||||
|  |                 ) | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         stdOutBuffer.ToString().Trim().Should().ConsistOfLines("Started.", "Cancelled."); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_configure_the_command_to_listen_to_the_interrupt_signal_when_running_in_isolation() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 public async ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     try | ||||||
|  |                     { | ||||||
|  |                         console.WriteLine("Started."); | ||||||
|  |  | ||||||
|  |                         await Task.Delay( | ||||||
|  |                             TimeSpan.FromSeconds(3), | ||||||
|  |                             console.RegisterCancellationHandler() | ||||||
|  |                         ); | ||||||
|  |  | ||||||
|  |                         console.WriteLine("Completed."); | ||||||
|  |                     } | ||||||
|  |                     catch (OperationCanceledException) | ||||||
|  |                     { | ||||||
|  |                         console.WriteLine("Cancelled."); | ||||||
|  |                         throw; | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|         var application = new CliApplicationBuilder() |         var application = new CliApplicationBuilder() | ||||||
|                 .AddCommand(typeof(CancellableCommand)) |             .AddCommand(commandType) | ||||||
|                 .UseConsole(console) |             .UseConsole(FakeConsole) | ||||||
|             .Build(); |             .Build(); | ||||||
|  |  | ||||||
|  |         FakeConsole.RequestCancellation(TimeSpan.FromSeconds(0.2)); | ||||||
|  |  | ||||||
|         // Act |         // Act | ||||||
|             cts.CancelAfter(TimeSpan.FromSeconds(0.2)); |  | ||||||
|  |  | ||||||
|         var exitCode = await application.RunAsync( |         var exitCode = await application.RunAsync( | ||||||
|                 new[] {"cancel"}, |             [], | ||||||
|                 new Dictionary<string, string>()); |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|             var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd(); |  | ||||||
|  |  | ||||||
|         // Assert |         // Assert | ||||||
|         exitCode.Should().NotBe(0); |         exitCode.Should().NotBe(0); | ||||||
|             stdOutData.Should().Be("Cancellation requested"); |  | ||||||
|         } |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().ConsistOfLines("Started.", "Cancelled."); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -1,13 +1,7 @@ | |||||||
| <Project Sdk="Microsoft.NET.Sdk"> | <Project Sdk="Microsoft.NET.Sdk"> | ||||||
|   <Import Project="../CliFx.props" /> |  | ||||||
|  |  | ||||||
|   <PropertyGroup> |   <PropertyGroup> | ||||||
|     <TargetFramework>netcoreapp3.1</TargetFramework> |     <TargetFramework>net9.0</TargetFramework> | ||||||
|     <IsPackable>false</IsPackable> |  | ||||||
|     <IsTestProject>true</IsTestProject> |  | ||||||
|     <CollectCoverage>true</CollectCoverage> |  | ||||||
|     <CoverletOutputFormat>opencover</CoverletOutputFormat> |  | ||||||
|     <CoverletOutput>bin/$(Configuration)/Coverage.xml</CoverletOutput> |  | ||||||
|   </PropertyGroup> |   </PropertyGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
| @@ -15,13 +9,18 @@ | |||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
|     <PackageReference Include="CliWrap" Version="3.0.0" /> |     <PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.8.3" /> | ||||||
|     <PackageReference Include="FluentAssertions" Version="5.10.2" /> |     <PackageReference Include="CliWrap" Version="3.9.0" /> | ||||||
|     <PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" /> |     <PackageReference Include="coverlet.collector" Version="6.0.4" PrivateAssets="all" /> | ||||||
|     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> |     <PackageReference Include="CSharpier.MsBuild" Version="1.1.2" PrivateAssets="all" /> | ||||||
|     <PackageReference Include="xunit" Version="2.4.1" /> |     <PackageReference Include="FluentAssertions" Version="8.6.0" /> | ||||||
|     <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" /> |     <PackageReference Include="GitHubActionsTestLogger" Version="2.4.1" PrivateAssets="all" /> | ||||||
|     <PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" /> |     <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" /> | ||||||
|  |     <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.9" /> | ||||||
|  |     <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" /> | ||||||
|  |     <PackageReference Include="PolyShim" Version="1.15.0" PrivateAssets="all" /> | ||||||
|  |     <PackageReference Include="xunit" Version="2.9.3" /> | ||||||
|  |     <PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" PrivateAssets="all" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |   <ItemGroup> | ||||||
| @@ -29,12 +28,4 @@ | |||||||
|     <ProjectReference Include="..\CliFx\CliFx.csproj" /> |     <ProjectReference Include="..\CliFx\CliFx.csproj" /> | ||||||
|   </ItemGroup> |   </ItemGroup> | ||||||
|  |  | ||||||
|   <ItemGroup> |  | ||||||
|     <None Include="../CliFx.Tests.Dummy/bin/$(Configuration)/$(TargetFramework)/CliFx.Tests.Dummy.runtimeconfig.json"> |  | ||||||
|       <Link>CliFx.Tests.Dummy.runtimeconfig.json</Link> |  | ||||||
|       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |  | ||||||
|       <Visible>False</Visible> |  | ||||||
|     </None> |  | ||||||
|   </ItemGroup> |  | ||||||
|  |  | ||||||
| </Project> | </Project> | ||||||
| @@ -1,74 +1,204 @@ | |||||||
| using System; | using System; | ||||||
|  | using System.Collections.Generic; | ||||||
| using System.IO; | using System.IO; | ||||||
|  | using System.Text; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
|  | using CliFx.Infrastructure; | ||||||
|  | using CliFx.Tests.Utils; | ||||||
|  | using CliFx.Tests.Utils.Extensions; | ||||||
| using CliWrap; | using CliWrap; | ||||||
| using CliWrap.Buffered; | using CliWrap.Buffered; | ||||||
| using FluentAssertions; | using FluentAssertions; | ||||||
| using Xunit; | using Xunit; | ||||||
|  | using Xunit.Abstractions; | ||||||
|  |  | ||||||
| namespace CliFx.Tests | namespace CliFx.Tests; | ||||||
|  |  | ||||||
|  | public class ConsoleSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) | ||||||
| { | { | ||||||
|     public class ConsoleSpecs |     [Fact(Timeout = 15000)] | ||||||
|     { |     public async Task I_can_run_the_application_with_the_default_console_implementation_to_interact_with_the_system_console() | ||||||
|         [Fact] |  | ||||||
|         public async Task Real_implementation_of_console_maps_directly_to_system_console() |  | ||||||
|     { |     { | ||||||
|  |         // Can't verify our own console output, so using an external process for this test | ||||||
|  |  | ||||||
|         // Arrange |         // Arrange | ||||||
|             var command = "Hello world" | Cli.Wrap("dotnet") |         var command = | ||||||
|                 .WithArguments(a => a |             "Hello world" | Cli.Wrap(Dummy.Program.FilePath).WithArguments("console-test"); | ||||||
|                     .Add(Dummy.Program.Location) |  | ||||||
|                     .Add("console-test")); |  | ||||||
|  |  | ||||||
|         // Act |         // Act | ||||||
|         var result = await command.ExecuteBufferedAsync(); |         var result = await command.ExecuteBufferedAsync(); | ||||||
|  |  | ||||||
|         // Assert |         // Assert | ||||||
|             result.StandardOutput.TrimEnd().Should().Be("Hello world"); |         result.StandardOutput.Trim().Should().Be("Hello world"); | ||||||
|             result.StandardError.TrimEnd().Should().Be("Hello world"); |         result.StandardError.Trim().Should().Be("Hello world"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     [Fact] |     [Fact] | ||||||
|         public void Fake_implementation_of_console_can_be_used_to_execute_commands_in_isolation() |     public void I_can_run_the_application_on_a_system_with_a_custom_console_encoding_and_not_get_corrupted_output() | ||||||
|     { |     { | ||||||
|         // Arrange |         // Arrange | ||||||
|             using var stdIn = new MemoryStream(Console.InputEncoding.GetBytes("input")); |         using var buffer = new MemoryStream(); | ||||||
|             using var stdOut = new MemoryStream(); |         using var consoleWriter = new ConsoleWriter(FakeConsole, buffer, Encoding.UTF8); | ||||||
|             using var stdErr = new MemoryStream(); |  | ||||||
|  |  | ||||||
|             var console = new VirtualConsole( |  | ||||||
|                 input: stdIn, |  | ||||||
|                 output: stdOut, |  | ||||||
|                 error: stdErr); |  | ||||||
|  |  | ||||||
|         // Act |         // Act | ||||||
|             console.Output.Write("output"); |         consoleWriter.Write("Hello world"); | ||||||
|             console.Error.Write("error"); |         consoleWriter.Flush(); | ||||||
|  |  | ||||||
|             var stdInData = console.Input.ReadToEnd(); |         // Assert | ||||||
|             var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()); |         var outputBytes = buffer.ToArray(); | ||||||
|             var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()); |         outputBytes.Should().NotContain(Encoding.UTF8.GetPreamble()); | ||||||
|  |  | ||||||
|  |         var output = consoleWriter.Encoding.GetString(outputBytes); | ||||||
|  |         output.Should().Be("Hello world"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_run_the_application_with_the_fake_console_implementation_to_isolate_console_interactions() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|                     console.ResetColor(); |                     console.ResetColor(); | ||||||
|                     console.ForegroundColor = ConsoleColor.DarkMagenta; |                     console.ForegroundColor = ConsoleColor.DarkMagenta; | ||||||
|                     console.BackgroundColor = ConsoleColor.DarkMagenta; |                     console.BackgroundColor = ConsoleColor.DarkMagenta; | ||||||
|  |                     console.WindowWidth = 100; | ||||||
|  |                     console.WindowHeight = 25; | ||||||
|                     console.CursorLeft = 42; |                     console.CursorLeft = 42; | ||||||
|                     console.CursorTop = 24; |                     console.CursorTop = 24; | ||||||
|  |  | ||||||
|             // Assert |                     console.Output.WriteLine("Hello "); | ||||||
|             stdInData.Should().Be("input"); |                     console.Error.WriteLine("world!"); | ||||||
|             stdOutData.Should().Be("output"); |  | ||||||
|             stdErrData.Should().Be("error"); |  | ||||||
|  |  | ||||||
|             console.Input.Should().NotBeSameAs(Console.In); |                     return default; | ||||||
|             console.Output.Should().NotBeSameAs(Console.Out); |  | ||||||
|             console.Error.Should().NotBeSameAs(Console.Error); |  | ||||||
|  |  | ||||||
|             console.IsInputRedirected.Should().BeTrue(); |  | ||||||
|             console.IsOutputRedirected.Should().BeTrue(); |  | ||||||
|             console.IsErrorRedirected.Should().BeTrue(); |  | ||||||
|  |  | ||||||
|             console.ForegroundColor.Should().NotBe(Console.ForegroundColor); |  | ||||||
|             console.BackgroundColor.Should().NotBe(Console.BackgroundColor); |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             [], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         Console.OpenStandardInput().Should().NotBeSameAs(FakeConsole.Input.BaseStream); | ||||||
|  |         Console.OpenStandardOutput().Should().NotBeSameAs(FakeConsole.Output.BaseStream); | ||||||
|  |         Console.OpenStandardError().Should().NotBeSameAs(FakeConsole.Error.BaseStream); | ||||||
|  |  | ||||||
|  |         Console.ForegroundColor.Should().NotBe(ConsoleColor.DarkMagenta); | ||||||
|  |         Console.BackgroundColor.Should().NotBe(ConsoleColor.DarkMagenta); | ||||||
|  |  | ||||||
|  |         // This fails because tests don't spawn a console window | ||||||
|  |         //Console.WindowWidth.Should().Be(100); | ||||||
|  |         //Console.WindowHeight.Should().Be(25); | ||||||
|  |         //Console.CursorLeft.Should().NotBe(42); | ||||||
|  |         //Console.CursorTop.Should().NotBe(24); | ||||||
|  |  | ||||||
|  |         FakeConsole.IsInputRedirected.Should().BeTrue(); | ||||||
|  |         FakeConsole.IsOutputRedirected.Should().BeTrue(); | ||||||
|  |         FakeConsole.IsErrorRedirected.Should().BeTrue(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_run_the_application_with_the_fake_console_implementation_and_simulate_stream_interactions() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     var input = console.Input.ReadToEnd(); | ||||||
|  |                     console.Output.WriteLine(input); | ||||||
|  |                     console.Error.WriteLine(input); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         FakeConsole.WriteInput("Hello world"); | ||||||
|  |  | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             [], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("Hello world"); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr.Trim().Should().Be("Hello world"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_run_the_application_with_the_fake_console_implementation_and_simulate_key_presses() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(console.ReadKey().Key); | ||||||
|  |                     console.WriteLine(console.ReadKey().Key); | ||||||
|  |                     console.WriteLine(console.ReadKey().Key); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         FakeConsole.EnqueueKey(new ConsoleKeyInfo('0', ConsoleKey.D0, false, false, false)); | ||||||
|  |         FakeConsole.EnqueueKey(new ConsoleKeyInfo('a', ConsoleKey.A, false, false, false)); | ||||||
|  |         FakeConsole.EnqueueKey(new ConsoleKeyInfo('\0', ConsoleKey.Backspace, false, false, false)); | ||||||
|  |  | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             [], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().ConsistOfLines("D0", "A", "Backspace"); | ||||||
|  |     } | ||||||
| } | } | ||||||
							
								
								
									
										941
									
								
								CliFx.Tests/ConversionSpecs.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										941
									
								
								CliFx.Tests/ConversionSpecs.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,941 @@ | |||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.Threading.Tasks; | ||||||
|  | using CliFx.Tests.Utils; | ||||||
|  | using CliFx.Tests.Utils.Extensions; | ||||||
|  | using FluentAssertions; | ||||||
|  | using Xunit; | ||||||
|  | using Xunit.Abstractions; | ||||||
|  |  | ||||||
|  | namespace CliFx.Tests; | ||||||
|  |  | ||||||
|  | public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) | ||||||
|  | { | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_string_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public string? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("xyz"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_an_object_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public object? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("xyz"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_boolean_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public bool Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 [CommandOption('b')] | ||||||
|  |                 public bool Bar { get; init; } | ||||||
|  |  | ||||||
|  |                 [CommandOption('c')] | ||||||
|  |                 public bool Baz { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine("Foo = " + Foo); | ||||||
|  |                     console.WriteLine("Bar = " + Bar); | ||||||
|  |                     console.WriteLine("Baz = " + Baz); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "true", "-b", "false", "-c"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().ConsistOfLines("Foo = True", "Bar = False", "Baz = True"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_an_integer_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public int Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-f", "32"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("32"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_double_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public double Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo.ToString(CultureInfo.InvariantCulture)); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "32.14"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("32.14"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_DateTimeOffset_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public DateTimeOffset Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo.ToString("u", CultureInfo.InvariantCulture)); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "1995-04-28Z"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("1995-04-28 00:00:00Z"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_TimeSpan_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public TimeSpan Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo.ToString(null, CultureInfo.InvariantCulture)); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "12:34:56"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("12:34:56"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_an_enum_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             public enum CustomEnum { One = 1, Two = 2, Three = 3 } | ||||||
|  |  | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public CustomEnum Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine((int) Foo); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-f", "two"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("2"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_nullable_integer_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public int? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 [CommandOption('b')] | ||||||
|  |                 public int? Bar { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine("Foo = " + Foo); | ||||||
|  |                     console.WriteLine("Bar = " + Bar); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-b", "123"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().ConsistOfLines("Foo = ", "Bar = 123"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_nullable_enum_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             public enum CustomEnum { One = 1, Two = 2, Three = 3 } | ||||||
|  |  | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public CustomEnum? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 [CommandOption('b')] | ||||||
|  |                 public CustomEnum? Bar { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine("Foo = " + (int?) Foo); | ||||||
|  |                     console.WriteLine("Bar = " + (int?) Bar); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-b", "two"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().ConsistOfLines("Foo = ", "Bar = 2"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_string_constructable_object_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             public class CustomType | ||||||
|  |             { | ||||||
|  |                 public string Value { get; } | ||||||
|  |  | ||||||
|  |                 public CustomType(string value) => Value = value; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public CustomType? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo.Value); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("xyz"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_string_parsable_object_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             public class CustomTypeA | ||||||
|  |             { | ||||||
|  |                 public string Value { get; } | ||||||
|  |  | ||||||
|  |                 private CustomTypeA(string value) => Value = value; | ||||||
|  |  | ||||||
|  |                 public static CustomTypeA Parse(string value) => | ||||||
|  |                     new CustomTypeA(value); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             public class CustomTypeB | ||||||
|  |             { | ||||||
|  |                 public string Value { get; } | ||||||
|  |  | ||||||
|  |                 private CustomTypeB(string value) => Value = value; | ||||||
|  |  | ||||||
|  |                 public static CustomTypeB Parse(string value, IFormatProvider formatProvider) => | ||||||
|  |                     new CustomTypeB(value); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public CustomTypeA? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 [CommandOption('b')] | ||||||
|  |                 public CustomTypeB? Bar { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine("Foo = " + Foo.Value); | ||||||
|  |                     console.WriteLine("Bar = " + Bar.Value); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "hello", "-b", "world"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().ConsistOfLines("Foo = hello", "Bar = world"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_property_with_a_custom_converter() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             public class CustomConverter : BindingConverter<int> | ||||||
|  |             { | ||||||
|  |                 public override int Convert(string rawValue) => | ||||||
|  |                     rawValue.Length; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f', Converter = typeof(CustomConverter))] | ||||||
|  |                 public int Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "hello world"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be("11"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_string_array_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public string[]? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     foreach (var i in Foo) | ||||||
|  |                         console.WriteLine(i); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "one", "two", "three"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().ConsistOfLines("one", "two", "three"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_read_only_list_of_strings_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public IReadOnlyList<string>? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     foreach (var i in Foo) | ||||||
|  |                         console.WriteLine(i); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "one", "two", "three"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().ConsistOfLines("one", "two", "three"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_a_string_list_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public List<string>? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     foreach (var i in Foo) | ||||||
|  |                         console.WriteLine(i); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "one", "two", "three"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().ConsistOfLines("one", "two", "three"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_bind_a_parameter_or_an_option_to_an_integer_array_property() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public int[]? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     foreach (var i in Foo) | ||||||
|  |                         console.WriteLine(i); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "1", "13", "27"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().ConsistOfLines("1", "13", "27"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_try_to_bind_a_parameter_or_an_option_to_a_property_and_get_an_error_if_it_is_of_an_unsupported_type() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             public class CustomType | ||||||
|  |             { | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public CustomType? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => default; | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().NotBe(0); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr.Should().Contain("has an unsupported underlying property type"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_try_to_bind_a_parameter_or_an_option_to_a_non_scalar_property_and_get_an_error_if_it_is_of_an_unsupported_type() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             public class CustomType : IEnumerable<object> | ||||||
|  |             { | ||||||
|  |                 public IEnumerator<object> GetEnumerator() => Enumerable.Empty<object>().GetEnumerator(); | ||||||
|  |  | ||||||
|  |                 IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public CustomType? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => default; | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "one", "two"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().NotBe(0); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr.Should().Contain("has an unsupported underlying property type"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_try_to_bind_a_parameter_or_an_option_to_a_property_and_get_an_error_if_the_user_provides_an_invalid_value() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public int Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => default; | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["-f", "12.34"], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().NotBe(0); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr.Should().NotBeNullOrWhiteSpace(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_try_to_bind_a_parameter_or_an_option_to_a_property_and_get_an_error_if_a_custom_validator_fails() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             public class ValidatorA : BindingValidator<int> | ||||||
|  |             { | ||||||
|  |                 public override BindingValidationError Validate(int value) => Ok(); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             public class ValidatorB : BindingValidator<int> | ||||||
|  |             { | ||||||
|  |                 public override BindingValidationError Validate(int value) => Error("Hello world"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f', Validators = [typeof(ValidatorA), typeof(ValidatorB)])] | ||||||
|  |                 public int Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => default; | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-f", "12"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().NotBe(0); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr.Should().Contain("Hello world"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_try_to_bind_a_parameter_or_an_option_to_a_string_parsable_property_and_get_an_error_if_the_parsing_fails() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             public class CustomType | ||||||
|  |             { | ||||||
|  |                 public string Value { get; } | ||||||
|  |  | ||||||
|  |                 private CustomType(string value) => Value = value; | ||||||
|  |  | ||||||
|  |                 public static CustomType Parse(string value) => throw new Exception("Hello world"); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption('f')] | ||||||
|  |                 public CustomType? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => default; | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync(["-f", "bar"], new Dictionary<string, string>()); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().NotBe(0); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr.Should().Contain("Hello world"); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,37 +0,0 @@ | |||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class DependencyInjectionSpecs |  | ||||||
|     { |  | ||||||
|         [Command] |  | ||||||
|         private class WithoutDependenciesCommand : ICommand |  | ||||||
|         { |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private class DependencyA |  | ||||||
|         { |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         private class DependencyB |  | ||||||
|         { |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class WithDependenciesCommand : ICommand |  | ||||||
|         { |  | ||||||
|             private readonly DependencyA _dependencyA; |  | ||||||
|             private readonly DependencyB _dependencyB; |  | ||||||
|  |  | ||||||
|             public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB) |  | ||||||
|             { |  | ||||||
|                 _dependencyA = dependencyA; |  | ||||||
|                 _dependencyB = dependencyB; |  | ||||||
|             } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,63 +0,0 @@ | |||||||
| using CliFx.Exceptions; |  | ||||||
| using FluentAssertions; |  | ||||||
| using Xunit; |  | ||||||
| using Xunit.Abstractions; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class DependencyInjectionSpecs |  | ||||||
|     { |  | ||||||
|         private readonly ITestOutputHelper _output; |  | ||||||
|  |  | ||||||
|         public DependencyInjectionSpecs(ITestOutputHelper output) => _output = output; |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Default_type_activator_can_initialize_a_command_if_it_has_a_parameterless_constructor() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var activator = new DefaultTypeActivator(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var obj = activator.CreateInstance(typeof(WithoutDependenciesCommand)); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             obj.Should().BeOfType<WithoutDependenciesCommand>(); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Default_type_activator_cannot_initialize_a_command_if_it_does_not_have_a_parameterless_constructor() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var activator = new DefaultTypeActivator(); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand))); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Delegate_type_activator_can_initialize_a_command_using_a_custom_function() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var activator = new DelegateTypeActivator(_ => |  | ||||||
|                 new WithDependenciesCommand(new DependencyA(), new DependencyB())); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var obj = activator.CreateInstance(typeof(WithDependenciesCommand)); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             obj.Should().BeOfType<WithDependenciesCommand>(); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Delegate_type_activator_throws_if_the_underlying_function_returns_null() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var activator = new DelegateTypeActivator(_ => null!); |  | ||||||
|  |  | ||||||
|             // Act & assert |  | ||||||
|             var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand))); |  | ||||||
|             _output.WriteLine(ex.Message); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,14 +0,0 @@ | |||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class DirectivesSpecs |  | ||||||
|     { |  | ||||||
|         [Command("cmd")] |  | ||||||
|         private class NamedCommand : ICommand |  | ||||||
|         { |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,66 +1,91 @@ | |||||||
| using System; | using System; | ||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| using System.IO; |  | ||||||
| using System.Text; |  | ||||||
| using System.Threading; | using System.Threading; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| using CliFx.Tests.Internal; | using CliFx.Tests.Utils; | ||||||
|  | using CliFx.Tests.Utils.Extensions; | ||||||
| using CliWrap; | using CliWrap; | ||||||
| using CliWrap.Buffered; |  | ||||||
| using FluentAssertions; | using FluentAssertions; | ||||||
| using Xunit; | using Xunit; | ||||||
|  | using Xunit.Abstractions; | ||||||
|  |  | ||||||
| namespace CliFx.Tests | namespace CliFx.Tests; | ||||||
|  |  | ||||||
|  | public class DirectivesSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) | ||||||
| { | { | ||||||
|     public partial class DirectivesSpecs |     [Fact(Timeout = 15000)] | ||||||
|  |     public async Task I_can_use_the_debug_directive_to_make_the_application_wait_for_the_debugger_to_attach() | ||||||
|     { |     { | ||||||
|         [Fact] |  | ||||||
|         public async Task Debug_directive_can_be_specified_to_have_the_application_wait_until_debugger_is_attached() |  | ||||||
|         { |  | ||||||
|             // We can't actually attach a debugger in tests, so instead just cancel execution after some time |  | ||||||
|  |  | ||||||
|         // Arrange |         // Arrange | ||||||
|             using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)); |         using var cts = new CancellationTokenSource(); | ||||||
|             var stdOut = new StringBuilder(); |  | ||||||
|  |  | ||||||
|             var command = Cli.Wrap("dotnet") |         // We can't actually attach a debugger, but we can ensure that the process is waiting for one | ||||||
|                 .WithArguments(a => a |         void HandleStdOut(string line) | ||||||
|                     .Add(Dummy.Program.Location) |         { | ||||||
|                     .Add("[debug]")) |             // Kill the process once it writes the output we expect | ||||||
|                 .WithEnvironmentVariables(e => e |             if (line.Contains("Attach the debugger to", StringComparison.OrdinalIgnoreCase)) | ||||||
|                     .Set("ENV_TARGET", "Mars")) | stdOut; |                 cts.Cancel(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|             // Act |         var command = Cli.Wrap(Dummy.Program.FilePath).WithArguments("[debug]") | HandleStdOut; | ||||||
|             await command.ExecuteAsync(cts.Token).Task.IgnoreCancellation(); |  | ||||||
|             var stdOutData = stdOut.ToString(); |  | ||||||
|  |  | ||||||
|             // Assert |         // Act & assert | ||||||
|             stdOutData.Should().Contain("Attach debugger to"); |         try | ||||||
|  |         { | ||||||
|  |             await command.ExecuteAsync(cts.Token); | ||||||
|  |         } | ||||||
|  |         catch (OperationCanceledException ex) when (ex.CancellationToken == cts.Token) | ||||||
|  |         { | ||||||
|  |             // This means that the process was killed after it wrote the expected output | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     [Fact] |     [Fact] | ||||||
|         public async Task Preview_directive_can_be_specified_to_print_provided_arguments_as_they_were_parsed() |     public async Task I_can_use_the_preview_directive_to_make_the_application_print_the_parsed_command_input() | ||||||
|     { |     { | ||||||
|         // Arrange |         // Arrange | ||||||
|             await using var stdOut = new MemoryStream(); |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|             var console = new VirtualConsole(output: stdOut); |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command("cmd")] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => default; | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|         var application = new CliApplicationBuilder() |         var application = new CliApplicationBuilder() | ||||||
|                 .AddCommand(typeof(NamedCommand)) |             .AddCommand(commandType) | ||||||
|                 .UseConsole(console) |             .UseConsole(FakeConsole) | ||||||
|             .AllowPreviewMode() |             .AllowPreviewMode() | ||||||
|             .Build(); |             .Build(); | ||||||
|  |  | ||||||
|         // Act |         // Act | ||||||
|         var exitCode = await application.RunAsync( |         var exitCode = await application.RunAsync( | ||||||
|                 new[] {"[preview]", "cmd", "param", "-abc", "--option", "foo"}, |             ["[preview]", "cmd", "param", "-abc", "--option", "foo"], | ||||||
|                 new Dictionary<string, string>()); |             new Dictionary<string, string> { ["ENV_QOP"] = "hello", ["ENV_KIL"] = "world" } | ||||||
|  |         ); | ||||||
|             var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd(); |  | ||||||
|  |  | ||||||
|         // Assert |         // Assert | ||||||
|         exitCode.Should().Be(0); |         exitCode.Should().Be(0); | ||||||
|             stdOutData.Should().ContainAll("cmd", "<param>", "[-a]", "[-b]", "[-c]", "[--option \"foo\"]"); |  | ||||||
|         } |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut | ||||||
|  |             .Should() | ||||||
|  |             .ContainAllInOrder( | ||||||
|  |                 "cmd", | ||||||
|  |                 "<param>", | ||||||
|  |                 "[-a]", | ||||||
|  |                 "[-b]", | ||||||
|  |                 "[-c]", | ||||||
|  |                 "[--option \"foo\"]", | ||||||
|  |                 "ENV_QOP", | ||||||
|  |                 "=", | ||||||
|  |                 "\"hello\"", | ||||||
|  |                 "ENV_KIL", | ||||||
|  |                 "=", | ||||||
|  |                 "\"world\"" | ||||||
|  |             ); | ||||||
|     } |     } | ||||||
| } | } | ||||||
							
								
								
									
										160
									
								
								CliFx.Tests/EnvironmentSpecs.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										160
									
								
								CliFx.Tests/EnvironmentSpecs.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,160 @@ | |||||||
|  | using System.Collections.Generic; | ||||||
|  | using System.IO; | ||||||
|  | using System.Threading.Tasks; | ||||||
|  | using CliFx.Tests.Utils; | ||||||
|  | using CliFx.Tests.Utils.Extensions; | ||||||
|  | using CliWrap; | ||||||
|  | using CliWrap.Buffered; | ||||||
|  | using FluentAssertions; | ||||||
|  | using Xunit; | ||||||
|  | using Xunit.Abstractions; | ||||||
|  |  | ||||||
|  | namespace CliFx.Tests; | ||||||
|  |  | ||||||
|  | public class EnvironmentSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) | ||||||
|  | { | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_configure_an_option_to_fall_back_to_an_environment_variable_if_the_user_does_not_provide_the_corresponding_argument() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption("foo", EnvironmentVariable = "ENV_FOO")] | ||||||
|  |                 public string? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 [CommandOption("bar", EnvironmentVariable = "ENV_BAR")] | ||||||
|  |                 public string? Bar { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo); | ||||||
|  |                     console.WriteLine(Bar); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             ["--foo", "42"], | ||||||
|  |             new Dictionary<string, string> { ["ENV_FOO"] = "100", ["ENV_BAR"] = "200" } | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().ConsistOfLines("42", "200"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_configure_an_option_bound_to_a_non_scalar_property_to_fall_back_to_an_environment_variable_if_the_user_does_not_provide_the_corresponding_argument() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption("foo", EnvironmentVariable = "ENV_FOO")] | ||||||
|  |                 public IReadOnlyList<string>? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     foreach (var i in Foo) | ||||||
|  |                         console.WriteLine(i); | ||||||
|  |  | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             [], | ||||||
|  |             new Dictionary<string, string> { ["ENV_FOO"] = $"bar{Path.PathSeparator}baz" } | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().ConsistOfLines("bar", "baz"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_configure_an_option_bound_to_a_scalar_property_to_fall_back_to_an_environment_variable_while_ignoring_path_separators() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 [CommandOption("foo", EnvironmentVariable = "ENV_FOO")] | ||||||
|  |                 public string? Foo { get; init; } | ||||||
|  |  | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) | ||||||
|  |                 { | ||||||
|  |                     console.WriteLine(Foo); | ||||||
|  |                     return default; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             [], | ||||||
|  |             new Dictionary<string, string> { ["ENV_FOO"] = $"bar{Path.PathSeparator}baz" } | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Trim().Should().Be($"bar{Path.PathSeparator}baz"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact(Timeout = 15000)] | ||||||
|  |     public async Task I_can_run_the_application_and_it_will_resolve_all_required_environment_variables_automatically() | ||||||
|  |     { | ||||||
|  |         // Ensures that the environment variables are properly obtained from | ||||||
|  |         // System.Environment when they are not provided explicitly to CliApplication. | ||||||
|  |  | ||||||
|  |         // Arrange | ||||||
|  |         var command = Cli.Wrap(Dummy.Program.FilePath) | ||||||
|  |             .WithArguments("env-test") | ||||||
|  |             .WithEnvironmentVariables(e => e.Set("ENV_TARGET", "Mars")); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var result = await command.ExecuteBufferedAsync(); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         result.StandardOutput.Trim().Should().Be("Hello Mars!"); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -1,27 +0,0 @@ | |||||||
| using System.Collections.Generic; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class EnvironmentVariablesSpecs |  | ||||||
|     { |  | ||||||
|         [Command] |  | ||||||
|         private class EnvironmentVariableCollectionCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("opt", EnvironmentVariableName = "ENV_OPT")] |  | ||||||
|             public IReadOnlyList<string>? Option { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command] |  | ||||||
|         private class EnvironmentVariableCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("opt", EnvironmentVariableName = "ENV_OPT")] |  | ||||||
|             public string? Option { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,93 +0,0 @@ | |||||||
| using System.Collections.Generic; |  | ||||||
| using System.IO; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Domain; |  | ||||||
| using CliFx.Tests.Internal; |  | ||||||
| using CliWrap; |  | ||||||
| using CliWrap.Buffered; |  | ||||||
| using FluentAssertions; |  | ||||||
| using Xunit; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class EnvironmentVariablesSpecs |  | ||||||
|     { |  | ||||||
|         // This test uses a real application to make sure environment variables are actually read correctly |  | ||||||
|         [Fact] |  | ||||||
|         public async Task Option_can_use_a_specific_environment_variable_as_fallback() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var command = Cli.Wrap("dotnet") |  | ||||||
|                 .WithArguments(a => a |  | ||||||
|                     .Add(Dummy.Program.Location)) |  | ||||||
|                 .WithEnvironmentVariables(e => e |  | ||||||
|                     .Set("ENV_TARGET", "Mars")); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             stdOut.TrimEnd().Should().Be("Hello Mars!"); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         // This test uses a real application to make sure environment variables are actually read correctly |  | ||||||
|         [Fact] |  | ||||||
|         public async Task Option_only_uses_environment_variable_as_fallback_if_the_value_was_not_directly_provided() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var command = Cli.Wrap("dotnet") |  | ||||||
|                 .WithArguments(a => a |  | ||||||
|                     .Add(Dummy.Program.Location) |  | ||||||
|                     .Add("--target") |  | ||||||
|                     .Add("Jupiter")) |  | ||||||
|                 .WithEnvironmentVariables(e => e |  | ||||||
|                     .Set("ENV_TARGET", "Mars")); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             stdOut.TrimEnd().Should().Be("Hello Jupiter!"); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Option_of_non_scalar_type_can_take_multiple_separated_values_from_an_environment_variable() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = CommandInput.Empty; |  | ||||||
|             var envVars = new Dictionary<string, string> |  | ||||||
|             { |  | ||||||
|                 ["ENV_OPT"] = $"foo{Path.PathSeparator}bar" |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<EnvironmentVariableCollectionCommand>(input, envVars); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand |  | ||||||
|             { |  | ||||||
|                 Option = new[] {"foo", "bar"} |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public void Option_of_scalar_type_can_only_take_a_single_value_from_an_environment_variable_even_if_it_contains_separators() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             var input = CommandInput.Empty; |  | ||||||
|             var envVars = new Dictionary<string, string> |  | ||||||
|             { |  | ||||||
|                 ["ENV_OPT"] = $"foo{Path.PathSeparator}bar" |  | ||||||
|             }; |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var instance = CommandHelper.ResolveCommand<EnvironmentVariableCommand>(input, envVars); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             instance.Should().BeEquivalentTo(new EnvironmentVariableCommand |  | ||||||
|             { |  | ||||||
|                 Option = $"foo{Path.PathSeparator}bar" |  | ||||||
|             }); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,34 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
| using CliFx.Exceptions; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class ErrorReportingSpecs |  | ||||||
|     { |  | ||||||
|         [Command("exc")] |  | ||||||
|         private class GenericExceptionCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("msg", 'm')] |  | ||||||
|             public string? Message { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("exc")] |  | ||||||
|         private class CommandExceptionCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("code", 'c')] |  | ||||||
|             public int ExitCode { get; set; } = 1337; |  | ||||||
|  |  | ||||||
|             [CommandOption("msg", 'm')] |  | ||||||
|             public string? Message { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("show-help")] |  | ||||||
|             public bool ShowHelp { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode, ShowHelp); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,172 +1,209 @@ | |||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| using System.IO; |  | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
|  | using CliFx.Tests.Utils; | ||||||
|  | using CliFx.Tests.Utils.Extensions; | ||||||
| using FluentAssertions; | using FluentAssertions; | ||||||
| using Xunit; | using Xunit; | ||||||
| using Xunit.Abstractions; | using Xunit.Abstractions; | ||||||
|  |  | ||||||
| namespace CliFx.Tests | namespace CliFx.Tests; | ||||||
|  |  | ||||||
|  | public class ErrorReportingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) | ||||||
| { | { | ||||||
|     public partial class ErrorReportingSpecs |  | ||||||
|     { |  | ||||||
|         private readonly ITestOutputHelper _output; |  | ||||||
|  |  | ||||||
|         public ErrorReportingSpecs(ITestOutputHelper output) => _output = output; |  | ||||||
|  |  | ||||||
|     [Fact] |     [Fact] | ||||||
|         public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_error_message_and_stack_trace() |     public async Task I_can_throw_an_exception_in_a_command_to_report_an_error_with_a_stacktrace() | ||||||
|     { |     { | ||||||
|         // Arrange |         // Arrange | ||||||
|             await using var stdErr = new MemoryStream(); |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|             var console = new VirtualConsole(error: stdErr); |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => | ||||||
|  |                     throw new Exception("Something went wrong"); | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|         var application = new CliApplicationBuilder() |         var application = new CliApplicationBuilder() | ||||||
|                 .AddCommand(typeof(GenericExceptionCommand)) |             .AddCommand(commandType) | ||||||
|                 .UseConsole(console) |             .UseConsole(FakeConsole) | ||||||
|             .Build(); |             .Build(); | ||||||
|  |  | ||||||
|         // Act |         // Act | ||||||
|         var exitCode = await application.RunAsync( |         var exitCode = await application.RunAsync( | ||||||
|                 new[] {"exc", "-m", "Kaput"}, |             [], | ||||||
|                 new Dictionary<string, string>()); |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|             var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd(); |  | ||||||
|  |  | ||||||
|         // Assert |         // Assert | ||||||
|         exitCode.Should().NotBe(0); |         exitCode.Should().NotBe(0); | ||||||
|             stdErrData.Should().ContainAll( |  | ||||||
|                 "System.Exception:", |  | ||||||
|                 "Kaput", "at", |  | ||||||
|                 "CliFx.Tests"); |  | ||||||
|  |  | ||||||
|             _output.WriteLine(stdErrData); |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().BeEmpty(); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr | ||||||
|  |             .Should() | ||||||
|  |             .ContainAllInOrder("System.Exception", "Something went wrong", "at", "CliFx."); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     [Fact] |     [Fact] | ||||||
|         public async Task Command_may_throw_a_specialized_exception_which_exits_with_custom_code_and_prints_minimal_error_details() |     public async Task I_can_throw_an_exception_with_an_inner_exception_in_a_command_to_report_an_error_with_a_stacktrace() | ||||||
|     { |     { | ||||||
|         // Arrange |         // Arrange | ||||||
|             await using var stdErr = new MemoryStream(); |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|             var console = new VirtualConsole(error: stdErr); |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => | ||||||
|  |                     throw new Exception("Something went wrong", new Exception("Another exception")); | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|         var application = new CliApplicationBuilder() |         var application = new CliApplicationBuilder() | ||||||
|                 .AddCommand(typeof(CommandExceptionCommand)) |             .AddCommand(commandType) | ||||||
|                 .UseConsole(console) |             .UseConsole(FakeConsole) | ||||||
|             .Build(); |             .Build(); | ||||||
|  |  | ||||||
|         // Act |         // Act | ||||||
|         var exitCode = await application.RunAsync( |         var exitCode = await application.RunAsync( | ||||||
|                 new[] {"exc", "-m", "Kaput", "-c", "69"}, |             [], | ||||||
|                 new Dictionary<string, string>()); |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|             var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd(); |         // Assert | ||||||
|  |         exitCode.Should().NotBe(0); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().BeEmpty(); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr | ||||||
|  |             .Should() | ||||||
|  |             .ContainAllInOrder( | ||||||
|  |                 "System.Exception", | ||||||
|  |                 "Something went wrong", | ||||||
|  |                 "System.Exception", | ||||||
|  |                 "Another exception", | ||||||
|  |                 "at", | ||||||
|  |                 "CliFx." | ||||||
|  |             ); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_throw_an_exception_in_a_command_to_report_an_error_and_exit_with_the_specified_code() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => | ||||||
|  |                     throw new CommandException("Something went wrong", 69); | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             [], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|         // Assert |         // Assert | ||||||
|         exitCode.Should().Be(69); |         exitCode.Should().Be(69); | ||||||
|             stdErrData.Should().Be("Kaput"); |  | ||||||
|  |  | ||||||
|             _output.WriteLine(stdErrData); |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().BeEmpty(); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr.Trim().Should().Be("Something went wrong"); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     [Fact] |     [Fact] | ||||||
|         public async Task Command_may_throw_a_specialized_exception_without_error_message_which_exits_and_prints_full_error_details() |     public async Task I_can_throw_an_exception_without_a_message_in_a_command_to_report_an_error_with_a_stacktrace() | ||||||
|     { |     { | ||||||
|         // Arrange |         // Arrange | ||||||
|             await using var stdErr = new MemoryStream(); |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|             var console = new VirtualConsole(error: stdErr); |             // lang=csharp | ||||||
|  |             """ | ||||||
|             var application = new CliApplicationBuilder() |             [Command] | ||||||
|                 .AddCommand(typeof(CommandExceptionCommand)) |             public class Command : ICommand | ||||||
|                 .UseConsole(console) |             { | ||||||
|                 .Build(); |                 public ValueTask ExecuteAsync(IConsole console) => | ||||||
|  |                     throw new CommandException("", 69); | ||||||
|             // Act |  | ||||||
|             var exitCode = await application.RunAsync( |  | ||||||
|                 new[] {"exc"}, |  | ||||||
|                 new Dictionary<string, string>()); |  | ||||||
|  |  | ||||||
|             var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd(); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             exitCode.Should().NotBe(0); |  | ||||||
|             stdErrData.Should().ContainAll( |  | ||||||
|                 "CliFx.Exceptions.CommandException:", |  | ||||||
|                 "at", |  | ||||||
|                 "CliFx.Tests"); |  | ||||||
|  |  | ||||||
|             _output.WriteLine(stdErrData); |  | ||||||
|             } |             } | ||||||
|  |             """ | ||||||
|         [Fact] |  | ||||||
|         public async Task Command_may_throw_a_specialized_exception_which_exits_and_prints_help_text() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             await using var stdOut = new MemoryStream(); |  | ||||||
|             await using var stdErr = new MemoryStream(); |  | ||||||
|  |  | ||||||
|             var console = new VirtualConsole(output: stdOut, error: stdErr); |  | ||||||
|  |  | ||||||
|             var application = new CliApplicationBuilder() |  | ||||||
|                 .AddCommand(typeof(CommandExceptionCommand)) |  | ||||||
|                 .UseConsole(console) |  | ||||||
|                 .Build(); |  | ||||||
|  |  | ||||||
|             // Act |  | ||||||
|             var exitCode = await application.RunAsync( |  | ||||||
|                 new[] {"exc", "-m", "Kaput", "--show-help"}, |  | ||||||
|                 new Dictionary<string, string>()); |  | ||||||
|  |  | ||||||
|             var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd(); |  | ||||||
|             var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd(); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             exitCode.Should().NotBe(0); |  | ||||||
|             stdErrData.Should().Be("Kaput"); |  | ||||||
|  |  | ||||||
|             stdOutData.Should().ContainAll( |  | ||||||
|                 "Usage", |  | ||||||
|                 "Options", |  | ||||||
|                 "-h|--help", "Shows help text." |  | ||||||
|         ); |         ); | ||||||
|  |  | ||||||
|             _output.WriteLine(stdErrData); |  | ||||||
|             _output.WriteLine(stdOutData); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Fact] |  | ||||||
|         public async Task Command_shows_help_text_on_invalid_user_input() |  | ||||||
|         { |  | ||||||
|             // Arrange |  | ||||||
|             await using var stdOut = new MemoryStream(); |  | ||||||
|             await using var stdErr = new MemoryStream(); |  | ||||||
|             var console = new VirtualConsole(output: stdOut, error: stdErr); |  | ||||||
|  |  | ||||||
|         var application = new CliApplicationBuilder() |         var application = new CliApplicationBuilder() | ||||||
|                 .AddCommand(typeof(CommandExceptionCommand)) |             .AddCommand(commandType) | ||||||
|                 .UseConsole(console) |             .UseConsole(FakeConsole) | ||||||
|             .Build(); |             .Build(); | ||||||
|  |  | ||||||
|         // Act |         // Act | ||||||
|         var exitCode = await application.RunAsync( |         var exitCode = await application.RunAsync( | ||||||
|                 new[] {"not-a-valid-command", "-r", "foo"}, |             [], | ||||||
|                 new Dictionary<string, string>()); |             new Dictionary<string, string>() | ||||||
|  |  | ||||||
|             var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd(); |  | ||||||
|             var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd(); |  | ||||||
|  |  | ||||||
|             // Assert |  | ||||||
|             exitCode.Should().NotBe(0); |  | ||||||
|             stdErrData.Should().NotBeNullOrWhiteSpace(); |  | ||||||
|  |  | ||||||
|             stdOutData.Should().ContainAll( |  | ||||||
|                 "Usage", |  | ||||||
|                 "[command]", |  | ||||||
|                 "Options", |  | ||||||
|                 "-h|--help", "Shows help text." |  | ||||||
|         ); |         ); | ||||||
|  |  | ||||||
|             _output.WriteLine(stdErrData); |         // Assert | ||||||
|             _output.WriteLine(stdOutData); |         exitCode.Should().Be(69); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().BeEmpty(); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr.Should().ContainAllInOrder("CliFx.Exceptions.CommandException", "at", "CliFx."); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     [Fact] | ||||||
|  |     public async Task I_can_throw_an_exception_in_a_command_to_report_an_error_and_print_the_help_text() | ||||||
|  |     { | ||||||
|  |         // Arrange | ||||||
|  |         var commandType = DynamicCommandBuilder.Compile( | ||||||
|  |             // lang=csharp | ||||||
|  |             """ | ||||||
|  |             [Command] | ||||||
|  |             public class Command : ICommand | ||||||
|  |             { | ||||||
|  |                 public ValueTask ExecuteAsync(IConsole console) => | ||||||
|  |                     throw new CommandException("Something went wrong", 69, true); | ||||||
|  |             } | ||||||
|  |             """ | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         var application = new CliApplicationBuilder() | ||||||
|  |             .AddCommand(commandType) | ||||||
|  |             .UseConsole(FakeConsole) | ||||||
|  |             .SetDescription("This will be in help text") | ||||||
|  |             .Build(); | ||||||
|  |  | ||||||
|  |         // Act | ||||||
|  |         var exitCode = await application.RunAsync( | ||||||
|  |             [], | ||||||
|  |             new Dictionary<string, string>() | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         // Assert | ||||||
|  |         exitCode.Should().Be(69); | ||||||
|  |  | ||||||
|  |         var stdOut = FakeConsole.ReadOutputString(); | ||||||
|  |         stdOut.Should().Contain("This will be in help text"); | ||||||
|  |  | ||||||
|  |         var stdErr = FakeConsole.ReadErrorString(); | ||||||
|  |         stdErr.Trim().Should().Be("Something went wrong"); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -1,155 +0,0 @@ | |||||||
| using System; |  | ||||||
| using System.Collections.Generic; |  | ||||||
| using System.Threading.Tasks; |  | ||||||
| using CliFx.Attributes; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests |  | ||||||
| { |  | ||||||
|     public partial class HelpTextSpecs |  | ||||||
|     { |  | ||||||
|         [Command(Description = "DefaultCommand description.")] |  | ||||||
|         private class DefaultCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("option-a", 'a', Description = "OptionA description.")] |  | ||||||
|             public string? OptionA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option-b", 'b', Description = "OptionB description.")] |  | ||||||
|             public string? OptionB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("cmd", Description = "NamedCommand description.")] |  | ||||||
|         private class NamedCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandParameter(0, Name = "param-a", Description = "ParameterA description.")] |  | ||||||
|             public string? ParameterA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option-c", 'c', Description = "OptionC description.")] |  | ||||||
|             public string? OptionC { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option-d", 'd', Description = "OptionD description.")] |  | ||||||
|             public string? OptionD { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("cmd sub", Description = "NamedSubCommand description.")] |  | ||||||
|         private class NamedSubCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandParameter(0, Name = "param-b", Description = "ParameterB description.")] |  | ||||||
|             public string? ParameterB { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandParameter(1, Name = "param-c", Description = "ParameterC description.")] |  | ||||||
|             public string? ParameterC { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option-e", 'e', Description = "OptionE description.")] |  | ||||||
|             public string? OptionE { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("cmd-with-params")] |  | ||||||
|         private class ParametersCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandParameter(0, Name = "first")] |  | ||||||
|             public string? ParameterA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandParameter(10)] |  | ||||||
|             public int? ParameterB { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandParameter(20, Description = "A list of numbers", Name = "third list")] |  | ||||||
|             public IEnumerable<int>? ParameterC { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option", 'o')] |  | ||||||
|             public string? Option { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("cmd-with-req-opts")] |  | ||||||
|         private class RequiredOptionsCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("option-a", 'a', IsRequired = true)] |  | ||||||
|             public string? OptionA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option-b", 'b', IsRequired = true)] |  | ||||||
|             public IEnumerable<int>? OptionB { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option-c", 'c')] |  | ||||||
|             public string? OptionC { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("cmd-with-enum-args")] |  | ||||||
|         private class EnumArgumentsCommand : ICommand |  | ||||||
|         { |  | ||||||
|             public enum CustomEnum { Value1, Value2, Value3 }; |  | ||||||
|  |  | ||||||
|             [CommandParameter(0, Name = "value", Description = "Enum parameter.")] |  | ||||||
|             public CustomEnum ParamA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("value", Description = "Enum option.", IsRequired = true)] |  | ||||||
|             public CustomEnum OptionA { get; set; } = CustomEnum.Value1; |  | ||||||
|  |  | ||||||
|             [CommandOption("nullable-value", Description = "Nullable enum option.")] |  | ||||||
|             public CustomEnum? OptionB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("cmd-with-env-vars")] |  | ||||||
|         private class EnvironmentVariableCommand : ICommand |  | ||||||
|         { |  | ||||||
|             [CommandOption("option-a", 'a', IsRequired = true, EnvironmentVariableName = "ENV_OPT_A")] |  | ||||||
|             public string? OptionA { get; set; } |  | ||||||
|  |  | ||||||
|             [CommandOption("option-b", 'b', EnvironmentVariableName = "ENV_OPT_B")] |  | ||||||
|             public string? OptionB { get; set; } |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         [Command("cmd-with-defaults")] |  | ||||||
|         private class ArgumentsWithDefaultValuesCommand : ICommand |  | ||||||
|         { |  | ||||||
|             public enum CustomEnum { Value1, Value2, Value3 }; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Object))] |  | ||||||
|             public object? Object { get; set; } = 42; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(String))] |  | ||||||
|             public string? String { get; set; } = "foo"; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(EmptyString))] |  | ||||||
|             public string EmptyString { get; set; } = ""; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Bool))] |  | ||||||
|             public bool Bool { get; set; } = true; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Char))] |  | ||||||
|             public char Char { get; set; } = 't'; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Int))] |  | ||||||
|             public int Int { get; set; } = 1337; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(TimeSpan))] |  | ||||||
|             public TimeSpan TimeSpan { get; set; } = TimeSpan.FromMinutes(123); |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(Enum))] |  | ||||||
|             public CustomEnum Enum { get; set; } = CustomEnum.Value2; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(IntNullable))] |  | ||||||
|             public int? IntNullable { get; set; } = 1337; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(StringArray))] |  | ||||||
|             public string[]? StringArray { get; set; } = { "foo", "bar", "baz" }; |  | ||||||
|  |  | ||||||
|             [CommandOption(nameof(IntArray))] |  | ||||||
|             public int[]? IntArray { get; set; } = { 1, 2, 3 }; |  | ||||||
|  |  | ||||||
|             public ValueTask ExecuteAsync(IConsole console) => default; |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -1,23 +0,0 @@ | |||||||
| using System.Collections.Generic; |  | ||||||
| using CliFx.Domain; |  | ||||||
|  |  | ||||||
| namespace CliFx.Tests.Internal |  | ||||||
| { |  | ||||||
|     internal static class CommandHelper |  | ||||||
|     { |  | ||||||
|         public static TCommand ResolveCommand<TCommand>(CommandInput input, IReadOnlyDictionary<string, string> environmentVariables) |  | ||||||
|             where TCommand : ICommand, new() |  | ||||||
|         { |  | ||||||
|             var schema = CommandSchema.TryResolve(typeof(TCommand))!; |  | ||||||
|  |  | ||||||
|             var instance = new TCommand(); |  | ||||||
|             schema.Bind(instance, input, environmentVariables); |  | ||||||
|  |  | ||||||
|             return instance; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         public static TCommand ResolveCommand<TCommand>(CommandInput input) |  | ||||||
|             where TCommand : ICommand, new() => |  | ||||||
|             ResolveCommand<TCommand>(input, new Dictionary<string, string>()); |  | ||||||
|     } |  | ||||||
| } |  | ||||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user